Creating a for loop for variables in a single POST

Iā€™ve got the following pre-request script:

    var moment = require('moment');
    pm.environment.set('time0', moment().add(0, 'minutes').toISOString());
    pm.environment.set('time1', moment().add(1, 'minutes').toISOString());
    pm.environment.set('time2', moment().add(2, 'minutes').toISOString());
    pm.environment.set('time3', moment().add(3, 'minutes').toISOString());
    pm.environment.set('time4', moment().add(4, 'minutes').toISOString());
    pm.environment.set('time5', moment().add(5, 'minutes').toISOString());
    pm.environment.set('time6', moment().add(6, 'minutes').toISOString());
    pm.environment.set('time7', moment().add(7, 'minutes').toISOString());
    pm.environment.set('time8', moment().add(8, 'minutes').toISOString());
    pm.environment.set('time9', moment().add(9, 'minutes').toISOString());
    pm.environment.set('time10', moment().add(10, 'minutes').toISOString());

And the Body references those timeX variables:

 {
    "id": "ID1",
    "timestamp": "{{time0}}",
    "value": 10
  },
  {
    "id": "ID1",
    "timestamp": "{{time1}}",
    "value": 12
  },
  {
    "id": "ID1",
    "timestamp": "{{time2}}",
    "value": 10
  },
    {
    "id": "ID1",
    "timestamp": "{{time3}}",
    "value": 8
  },

Iā€™m unsure how to turn this into a for loop that fills in the corresponding time variables in a single POST. It works as-is, but Iā€™m pretty sure thereā€™s a way to reduce the 70 time variables (from now-10 minutes to now+60 minutes) in the pre-request script to a loop. Anyone know how this can be done? Thanks.

Hi @dleenhouts! Try this:

for (var i = 0; i < 11; i++) { // change to i < 71 if you need 70 variables
    pm.environment.set('time'+i,moment().add(i, 'minutes').toISOString());
}

Thanks, thatā€™s good enough for the now to now+60 part. I tried (var i = -10; i < 61; i++) but I donā€™t think you can do something like .add(-10, ā€˜minutesā€™). Using a second loop with (var i = 0; i < 10; i++) & .subtract(i, ā€˜minutesā€™) worked for the now-10 to now variables.

@dleenhouts cool! Glad that helped!

Another approach would be declaring a starting timestamp that is -10 minutes from the current time, and adding to that each loop. That would allow you to use a single loop.

Is there a way to trim down this request to have a single object like:

{
    "id": "ID1",
    "timestamp": "{{time}}",
    "value": 10
}

With a loop that sets the {{time}} variable from now-10 minutes to now+60 minutes, rather than having a body with 70 variables? The loop Iā€™m trying to use only sets the {{time}} variable to the last value (71) rather than every value in the loop:

for (var i = 0; i < 71; i++) { 
    pm.environment.set('time'+i,moment().add(i, 'minutes').toISOString());
}
console.log(i);

Whoops, yeah thatā€™s my bad. Youā€™ll need to loop the request using postman.setNextRequest(), and store the value of i in the environment. Let me throw a working example of this together, and Iā€™ll share shortly.

@dleenhouts Sorry again about that. Click Run In Postman on the following docs page to get a working example of how to do this:

https://documenter.getpostman.com/view/5293839/SWTK4EWu

Thanks, think I got it.

BTW Iā€™m trying this reply via email since I cannot log into the Postman community website (click Login, select Google account, just redirects back to this topic. Tried clearing browsing data, different browser, different machine. Tried ā€œtrouble signing inā€ + sending pw reset email, never got the reset email). Any suggestions there?

@dleenhouts weā€™re currently tracking this issue and working on a fix. Hope to have it addressed soon.