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

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

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);
   = 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

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

In the result at console we can see that fucntion was executed as But at the same time no console.log output from update_name function was printed and collectionVariable was not updated.

Details (like screenshots):

How I found the problem:

I’ve already tried:

See the following example of setting an environment variable.

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

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

Tests tab

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

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

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?

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.

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.

1 Like