There was an error in evaluating the Pre-request Script: TypeError: Cannot read property 'shift' of undefined

Hi there!

I am new to Postman automation and facing some problem with running the test with different data set.

I am getting an error, “There was an error in evaluating the Pre-request Script: TypeError: Cannot read property ‘shift’ of undefined”

Pre-request script:

var Ids= pm.globals.get("buyerProgramId")

if(!(Ids)){

var buyerProgramId= [1961, 1962, 1972, 1976, 1977];

}

var currentId= buyerProgramId.shift();

pm.globals.set("buyerProgramId", buyerProgramId);

pm.globals.set("currentId",currentId)

TEST:

var Ids= pm.globals.get("buyerProgramId");

if(Ids && Ids.length>0){

postman.setNextRequest('Get Buyer Program Information by ID');

}else{

      postman.setNextRequest(null); 

}

Appreciate your help if someone can tell me what’s wrong here?

Thanks in advance!

Hello @vaibhav.sh.05 , Warm Welcome to the Community :clap:

buyerProgramId is a defined array ? if so why is it inside if loop? Can you please explain a little bit more here, what’s your expectation? And what are you trying to Test? Also kindly place your scripts under ``` so that it will be easy to copy paste and work for…

like this

var buyerProgramId= ["1961", "1962", "1972", "1976", "1977"];
var Ids= pm.globals.get("buyerProgramId")

if(!(Ids)){

var currentId= buyerProgramId.shift();
pm.globals.set("buyerProgramId", buyerProgramId);

pm.globals.set("currentId",currentId)

}
1 Like

Your if statement is evaluating to false, so buyerProgramId is never set.
I’d recommend changing what you have so a slightly different version than what @bpricilla said.

let buyerProgramIds = ['1961', '1962', '1972', '1976', '1977'];
try {
  buyerProgramIds = JSON.parse(pm.globals.get('buyerProgramIds'));
}
catch (err) {}

pm.globals.set('currentId', buyerProgramIds.shift());
pm.globals.set('buyerProgramIds', JSON.stringify(buyerProgramIds));

This defaults your list of Ids to the hardcoded list, but if there is already a list defined in the globals, uses that instead.

In your test script, just modify it slightly:

var buyerProgramIds = JSON.parse(pm.globals.get('buyerProgramIds'));

if(buyerProgramIds && buyerProgramIds.length){
  postman.setNextRequest('Get Buyer Program Information by ID');
} else{
  postman.setNextRequest(null); 
}
1 Like

Thanks @bpricilla for pointing that out. Actually, my task is to run the collection with a different dataset in each run(which is buyerProgramId in my case) until the size of the array.

So, I want to run the Postman test with 1st Iteration: 1961, 2nd Iteration: 1962, and so on.

I have now removed the array from the if condition. Now, I am no longer getting the above error but now, the request seems to be moving in an indefinite loop.
It is always running the request with the buyerProgramId 1961 instead of picking other IDs in the array.

Pre-request script:

var Ids= pm.globals.get(‘buyerProgramId’)
var buyerProgramId= [‘1961’, ‘1962’, ‘1972’, ‘1976’, ‘1977’];

if(!(Ids)){

var currentId= buyerProgramId.shift();

pm.globals.set(‘buyerProgramId’, buyerProgramId);
pm.globals.set(‘currentId’,currentId)
}

Tests:

var Ids= pm.globals.get(“buyerProgramId”);

if(Ids && Ids.length>0){

postman.setNextRequest(‘Get Buyer Program Information by ID’);

}else{

  postman.setNextRequest(null); 

}

Thanks @allen.helton for your feedback. I tried with what you have suggested but still I am not able to iterarte through each and every ID in the buyerProgramId Array.

With your version, It’s picking up any 1 random buyerProgramId in the array and running the iteration only once.

I want to check the results with each and every buyerProgramId. Can you please help here?

Pre-request script:

let buyerProgramId= [‘1961’, ‘1962’, ‘1972’, ‘1976’, ‘1977’];

try{

buyerProgramId= JSON.parse(pm.globals.get(‘buyerProgramId’))

}

catch(Exceptione){

false;

}

let currentId= buyerProgramId.shift();

pm.globals.set(‘buyerProgramId’, JSON.stringify(buyerProgramId));

pm.globals.set(‘currentId’,currentId);

Tests:

let Ids= JSON.parse(pm.globals.get(‘buyerProgramId’))

if(Ids && Ids.length>0){

postman.setNextRequest(‘Get Buyer Program Information by ID’);

}else{

  postman.setNextRequest(null); 

}

Are you running it in the collection runner? You have to do that if you want it to loop.

Running a single request manually will only ever run one request.

Yes, I am running it in collection runner. And the collection running is running the test for only 1 buyerProgramId in the array.

postman.setNextRequest(‘Get Buyer Program Information by ID’);

Does anything depend on this request name? Or, it could be any name?

@vdespa Can you please help here?

@vaibhav.sh.05 :wave: I think you are resetting the array back to its original state every time, hence the reason why the request only runs for the first element and since the array never gets exhausted, you are stuck in an infinite loop.

Check the answers here to figure out how to create a loop setNextRequest() doesn't work


You are calling Get Buyer Program Information by ID to perform the iteration, and if you are setting the buyerProgramId array either in this request or any request in between, the array will always be reset to its original state.

You need to structure your Collection in a way that the first request initializes your variables, and then the next requests are used for looping

I don’t think it’s resetting the array every time. It should be defaulting the array to the initial values, but then overwriting it with the value in the global variable.

I think where @vaibhav.sh.05 might be going wrong is the parameter he’s passing into postman.setNextRequest.

That value has to match the name of the request you want to run. If you want to make sure you loop back on the same request you are currently on, you could just do postman.setNextRequest(request.name)

1 Like

Hi @amit thanks for your feedback.

I tried with your suggestion but still, the request is not picking up all the IDs I have given in the Array.
It is running the request in collection runner for only the 1st ID i.e. ‘1961’.

Pre-request script:

var buyerProgramId= [‘1961’, ‘1962’, ‘1972’, ‘1976’, ‘1977’];

pm.globals.set(“buyerProgramId”, JSON.stringify(buyerProgramId));

Tests:

var Ids = JSON.parse(pm.globals.get(“buyerProgramId”));
var nextId= Ids.pop();
pm.globals.set(“Ids”, JSON.stringify(Ids));

if(nextId == ‘1972’){

postman.setNextRequest(null);

}else{

postman.setNextRequest(nextId);

}

Can you check if I am doing anything wrong here?

I used request.name in the setNextRequest and it is still running into an indefinite loop with the 1st ID in the array

Ok your last message was telling of what was wrong.

Your prerequest script in fact is resetting the array every time.

Pre-request script:

var buyerProgramId= [‘1961’, ‘1962’, ‘1972’, ‘1976’, ‘1977’];
pm.globals.set(“buyerProgramId”, JSON.stringify(buyerProgramId));

This is where you want to do your array manipulation. You should be popping the array and saving the new contents here.

let buyerProgramId = pm.globals.get('buyerProgramId');
if(!buyerProgramId){
  buyerProgramId= ['1961', '1962', '1972', '1976', '1977'];
}
else {
  buyerProgramId = JSON.parse(buyerProgramId);
}

let currentId = buyerProgramId.pop();
pm.globals.set('currentId', currentId);
pm.globals.set('buyerProgramId', JSON.stringify(buyerProgramId));

Then in your tests, you just need to evaluate if there are more items in the array

Tests:

let buyerProgramId = JSON.parse(pm.globals.get('buyerProgramId'));
if(buyerProgramId.length) {
  postman.setNextRequest(request.name);
} else {
  postman.setNextRequest(null);
}
2 Likes

Thanks @allen.helton. The array manipulation is now working in the way I wanted it to work.
This is a perfect solution! Thanks for all your efforts and help.

Appreciate it! Cheers

1 Like