# Function created in pre-request executed without console.log output

**URL:** <https://community.postman.com/t/function-created-in-pre-request-executed-without-console-log-output/43878>\
**Category:** Help Hub\
**Created:** [February 12, 2023, 3:14pm UTC](https://community.postman.com/t/function-created-in-pre-request-executed-without-console-log-output/43878 "2023-02-12T15:14:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ganamn](https://avatars.discourse-cdn.com/v4/letter/g/779978/32.png) [@Ganamn](https://community.postman.com/u/Ganamn)\
**Post date:** [February 12, 2023, 3:14pm UTC](https://community.postman.com/t/function-created-in-pre-request-executed-without-console-log-output/43878/1 "2023-02-12T15:14:01Z")

</div>

_My question_:  
Hi, colleagues  
Please help me to understand how it works

1. Why is console.log not printed?
2. Why is pm.collectionVariable NOT updated with new name?

In collection pre request script I created global object that has one function to update collectionVariable

```auto
if (typeof utils == "undefined") {
    console.log("START pre-request folder name=" + pm.collectionVariables.get("name"));
    utils = { 
        name: undefined,       
        update_name: function(name) {
            console.log("Execute update name function with name=" + name);
            console.log("BEFORE update_name function name=" + pm.collectionVariables.get("name"));
            pm.collectionVariables.set("name", name);
            this.name = name;
            console.log("AFTER update_name function name=" + pm.collectionVariables.get("name"));
        }
    }
}

console.log("END pre-request folder name=" + pm.collectionVariables.get("name"));

```

In request test script I executed the function _utils.update\_name_

```auto
console.log("START pre-request request name=" + pm.collectionVariables.get("name"));
pm.collectionVariables.set("name", "Max")
utils.update_name("Kate");
console.log("END pre-request request name=" + pm.collectionVariables.get("name"));

```

In the result at console we can see that fucntion was executed as _utils.name=Kate_. But at the same time no console.log output from _update\_name_ function was printed and _collectionVariable_ was not updated.

_Details (like screenshots)_:  
 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/2/2/22a74afa7afd9eb382a6abe2bd353dd12803dd0c.png)

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/a/6/a6596af901889a9d1870e52239058fd2c2cf1128.png)

_How I found the problem_:

_I’ve already tried_:

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [February 12, 2023, 4:29pm UTC](https://community.postman.com/t/function-created-in-pre-request-executed-without-console-log-output/43878/2 "2023-02-12T16:29:56Z")

</div>

See the following example of setting an environment variable.

Pre-request script. (Which creates the function as a global variable).

```auto
utils = {
    setEnv(myPm, envVar, val) {
        console.log(env + " : " + val)
        return myPm.environment.set(envVar, val);
    }
}

```

Tests tab

```auto
utils.setEnv(pm, 'testVariable', 'testValue');

```

You have to pass the pm value, which is what I think you are missing.

---

<div class="post-metadata">

**Author:** ![Ganamn](https://avatars.discourse-cdn.com/v4/letter/g/779978/32.png) [@Ganamn](https://community.postman.com/u/Ganamn)\
**Post date:** [February 12, 2023, 4:46pm UTC](https://community.postman.com/t/function-created-in-pre-request-executed-without-console-log-output/43878/3 "2023-02-12T16:46:17Z")

</div>

Thank you,  
I still don’t understand why it’s manadatory to pass _pm_ if it’s global. Also actually getting value of collection variables _pm.collectionVariables.get_ works fine in this function, but updates doesn’t.

And why the _console.log_ in this function doesn’t work?

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [February 13, 2023, 12:32pm UTC](https://community.postman.com/t/function-created-in-pre-request-executed-without-console-log-output/43878/4 "2023-02-13T12:32:18Z")

</div>

I don’t know why you have to pass PM, but that is what all of the examples I’ve seen have included.

Also not sure why the console.log is no longer working.

Apart from the typo, env vs envVar, its not showing in the logs. This used to work. The function itself is working.

I’ve just tested with another function using object prototype and that is also not showing values in the console logs either.

I tried the desktop client as well as the web client, and its failing in both environments.

Really strange, as all one function does is console log what you send it. This used to work. It was the first one I got working with Object Prototype.

Mmmm, need to have a look at this myself now.

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [February 13, 2023, 1:20pm UTC](https://community.postman.com/t/function-created-in-pre-request-executed-without-console-log-output/43878/5 "2023-02-13T13:20:25Z")

</div>

I don’t know what is going on here.

It’s obviously finding the function, as the function itself works ok.

It just won’t console log anything back.

I’ve tried defining a global variable (the usual way of doing this).  
I’ve tried using object prototype, but that has the same issue.  
I’ve even tried eval, which was the old way of doing this.

If I put the function into the Tests tab. Then it console logs as you would expect.

But when you put the function into the pre-request script where it belongs, it doesn’t error, but doesn’t console log either. The functions do work though (if they aren’t just console logging). The collection variables do get created.

If I console log the function name, I can see that Postman can see the functions inside.

I’m wondering if this is a new bug.

Fairly certain this used to work, and if you look at other examples on Stack Overflow, they have plenty of examples using console.log.

I don’t use this a lot, so don’t know when it broke.
