setNextRequest in loop not executing

I am trying to run the same request multiple times based on a JSON array. The issue I have is that it only executes the request once, like it’s ignoring the setNextRequest. I’ve seen a bunch of posts similar to this issue, but none of the suggestions seem to resolve my issue. I am running it via a Collection runner and it is not reacting as I expect. I have the log statement and it is displaying each iteration (0 and 1), but the “get vehicle” request only occurs once instead of twice as I would expect and it has the property value of “truck” so its only getting the value of the last item in the array. Any suggestions would be appreciated.

Here is my code that is in the Pre-Request Script:

let attributeArray = [{
    "cars": '{"type": "string"}'
},
{
    "trucks": '{"type": "string"}'
}
];

for (var i = 0; i < attributeArray.length; i++)
{
    var obj = attributeArray[i];

    for (var property in obj)
    {
        var propertyType = obj[property];

        pm.collectionVariables.set("attributeProperty", property);
        pm.collectionVariables.set("attributePropertyType", propertyType);

        postman.setNextRequest("get vehicle");

        console.log(i);
    }
}

Hi! I can see that you want to run a for loop inside pre-request script. Pre-request script/Tests script are executed before/after the request, i.e.
Pre-request script -> Request execution -> Tests script
So what’s happening here would be:

  1. for loop is executed fully in a pre-request script
  2. The request is sent
    That’s why only a single request is being sent.

So instead, what you can do is:

  1. Set an array as a collection variable in the Tests script of a previous request (e.g. pm.collectionVariables.set('myArray', myArray)) or as a initial value of collection variable
  2. Retrieve that array value from the collection variable, update it (by using shift or pop) and then repeat the request, e.g.
let myArray = pm.collectionVariables.get('myArray')

// Get first element from an array and update environmental values
const elem = myArray.shift()
pm.collectionVariables.set('myVar', elem)
pm.collectionVariables.set('myArray', myArray)

// If elem is not undefined, run the request again, otherwise stop
if (elem) {
postman.setNextRequest('GET Request')
} else {
postman.setNextRequest(null)
}

I hope this helps. Please let me know if you have any further question;)

let attributeArray = [{
    "cars": '{"type": "string"}'
},
{
    "trucks": '{"type": "string"}'
}
];

if(pm.info.iteration===0){

pm.environment.set("array",attributeArray)

}
let arr=pm.environment.get("array")

if (arr.length!==0)
{
    var obj = arr.pop()

pm.environment.set("array",arr)
    for (var property in obj)
    {
        var propertyType = obj[property];

        pm.collectionVariables.set("attributeProperty", property);
        pm.collectionVariables.set("attributePropertyType", propertyType);

        postman.setNextRequest("get vehicle");

        console.log(i);
    }
}
``