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!

1 Like

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 @allenheltondev 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 @allenheltondev. 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

Hi @vaibhav.sh.05 what is the body you used in request.name , wanted to check as i am running into error as well

Hi @vinay0010

I am not sure if I understand your question correctly. However, i am using setNextRequest to the same request as I wanted to loop in though all my buyer program IDs

Using this syntax: postman.setNextRequest(request.name)

And if you are talking about the data in body of postman request then I am not using any data in the body. I was getting the data by passing the path variable, id : {{currentId}}

where currentId I am getting from,
let currentId = buyerProgramId.shift(); (from the pre script)

Hope this helps!

Best,
Vaibhav Sharma

Hi @allan.helton
I need help. Below you can see my code, the problem is very close to your answer. The difference is in array.
I have generated an array of objects. It is big array and I should run the collection with a different dataset in each run until the size of the array.
I have three properties in my array’s objects - "r_type", "r_target", "u_range".

Pre-request script:

let global1 = ['01', '02', '03', '05']; 
let global2 = [1, 3, 4, 11];
let global3 = {};
global3['1'] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20];
global3['3'] = [31];
global3['4'] = [34];
global3['11'] = [54];

let requestsArray = [];
for (let u = 0; u < global1.length; u++)
{
    for (let j = 0; j < global2.length; j++)
    {
        for (let x = 0; x < global3[global2[j]].length; x++)
        {
            const obj = {};
            obj.r_type = global1[u];
            obj.r_target = global2[j];
            obj.u_range = global3[global2[j]][x];
            requestsArray.push(obj);
        }
    }
}
console.log(requestsArray);
let f = pm.globals.get("f");
if (!f || f.length == 0) {
    f = requestsArray;
}

let current = f.shift();
pm.globals.set("trio", current);
pm.globals.set("f", f);

Test script:

const f = pm.globals.get("f");

if (f && f.length > 0){
    postman.setNextRequest(pm.info.requestName);
} else {
    postman.setNextRequest(null);
}

How can I access to objects into array and use its properties as variables ?

I will be honest, this code is extremely hard to follow. I can’t figure out what its doing vs what you want it to do. Is there any way you could clarify, please?

Hi @allenheltondev , thanks for your feedback.
First of all I have generated an array of objects. Then I need to iterate through each object into this array and use object’s properties as variable.

console.log(requestsArray);

This is my array.

I have opened new topic in help category related to this question.
Please, let me know if you need more information.
Thanks in advance