Pre-request error - Cannot read property 'set' of undefined

I am trying to run my postman collection from my CLI using Newman, but I keep getting this error
TypeError in prerequest-script. Kindly put me through on what I am not doing correctly. Below is my pre-request script:

    var seed = Math.floor(Math.random() * 899999999999 + 100000000000);
    var d = new Date();
    var sessionID = '{{source_bank_code}}' + d.getFullYear().toString().substr(-2)+("0"+(d.getMonth()+1)).slice(-2)+("0" + d.getDate()).slice(-2)+("0" + d.getHours()).slice(-2) + ("0" + d.getMinutes()).slice(-2)+("0" + d.getSeconds()).slice(-2) + seed;

    var beneficiary_account_number = "2345678907";

    pm.collectionVariables.set("session_id", sessionID);
    pm.collectionVariables.set("beneficiary_account_number", beneficiary_account_number); 
    pm.collectionVariables.set("name_enquiry_ref", sessionID);

At the end of the test, the failure detail says,
01. TypeError: Cannot read property 'set' of undefined at prerequest-script inside "Source Bank Agent / Transfers / Account Enquiry / Get Account Details (Valid)"


I see two possible problems:

  1. pm.collectionVariables.set is a newly introduced API and you need to update Newman to the latest version. Otherwise, it will not work.

This is causing the error “TypeError: Cannot read property ‘set’ of undefined”. pm.collectionVariables is undefined as it does not exist yet in your version of Newman.

  1. Just using double curly braces in a string (like {{source_bank_code}}) won’t motivate Postman to replace them for you.

You can use the pm.* API to fetch the respective value.


Thanks for your response.

The version of newman that I run on is 4.5.4 which is pretty recent. Nevertheless, I’ll try to update it to the latest version (v4.5.6) and see if it would handle it differently.

As regard the use of the double curly bracket to place variables in the script, it works just fine with postman, so I assumed newman shouldn’t have an issue with it. But also, I will do well to explore that option of fetching the value first and placing it in a variable local to the script.

I have tried getting rid of the double curly bracket. The error still persists.

@conigbinde I tried your Pre-request script with newman 4.5.6 and everything worked without issue.

Do confirm your newman install version as follows:

$ newman --version

If you are still on an older version, update as follows

$ npm install -g newman

Thanks all @amit for the reply. After updating my newman to the latest version, it worked without any issue.
I was only able to use the following command to update my newman:

$ npm install -g newman

My bad, I did not realise that you where using the string in the request somewhere.

1 Like

My bad! I incorrectly typed postman instead of newman
But glad you were able to update newman and get it to work.

1 Like

Hi Amit/vdespa, When I am running from Poatman it’s working perfectly, But facing issue from running Newman. I am using Newman version 5.1.2

Here are my tests

responsedata = JSON.parse(responseBody)
value = responsedata.items[3].id

pm.environment.set("PlaylistIDBefore", value);
responsedata = JSON.parse(responseBody)
value = responsedata.items[0].track.uri

pm.test("Status code is 200", function () {
    var res = value.split(":");
    pm.environment.set("trackid", res[2]);

Hey @raghavpostmannewman

Welcome to the community! :wave:

Could you also please include the response body as the test code alone doesn’t really tell the whole story :smiley:

Any additional information about the request and the newman command you are using will also help others to figure out what’s going on here. :smiley:

@danny-dainton Here is more info

It looks like all of the Calls to spotify are unauthorized and returning a 401 statue code.

Check your Auth method for the requests.

Hi Raghav,

Do you managed to fix this error? I am getting similar error as well. Collection running through postman runner without any error, but in newman CLI getting error as Type error in script. I am using newman 5.3.0

@danny-dainton - Hello, I am also facing this similar error, its working fine on my postman but when I ran it with newman i got errors.

new man cli

Its working fine with postman runner

BTW: my newman version is 6.1.2

cc @karthickrajanrathina

Hey @naumanmalik ,

I have no contextual information here, just 2 screenshots. :sweat_smile:

Your error might be similar but your context is different. I don’t know what your workflow is here or what scripts you have in place that could be storing variables.

I don’t know what Newman command you’re using or if you’re using any environment files with that command. I don’t know if you have exported an environment file which might not contain values if you only have them in the Current Value.

You need to fully explain everything in your own context so that people will have the best chance to help you. :pray:t2:

Also, as this old post already had a solution, which you would have been told about that when you replied, I would recommend opening up a new topic when asking for help. It saves a lot of back and forth questions trying to clarify information.

Hi @danny-dainton, Apologies for the late response so i am adding all my steps in detail now.

Step 1: User login to the mobile app and I am saving the general details and access token of that user in the form of collection variable. BTW I also tried with Global or environment variable.

Step 2: Storing the Cart id after adding some food items in it.

Step 3: On the basis of that Cart id i am going to place an order with this checkout API.

This is all i am doing in postman and its working fine when I execute it by RUNNER at once. But its now working when I execute it from newman.

Command: newman run AUTOMATEDRUSHOUR.postman_collection.json


You need to check the status code of your first request, the login.

I suspect this is failing, which is then causing the rest of the requests to fail, which means quite rightly it can’t read the cardId variable.

You should be able to see any console logs in the Newman CLI when you run it, that should be your first troubleshooting step.