Getglobalvariable array of result

Asked

Viewed 88 times

0

Good evening, I’m trying to make a variable to use in another request and I’m testing.

I made an array with the code attribute coming from my payload, it contains 4 codes from 1 to 4 so my array is 4 positions. I made a variable to save Array values.

When I call Getglobal within the same function where I did this, the return of arrays is correct, from 1 to 4.

But if I call Getglobal in another method or outside the function it simply returns me all 4 and not the array with the positions, can anyone help me? I would like it to appear 1,2,3,4 and not only the number 4.

let responseJson = JSON.parse(responseBody);
let array = [];

pm.test("Testing",function()
{
    for(var value in responseJson)
    {
        trab = responseJson[value].code;
        pm.globals.set("testCode",trab);
        console.log(pm.globals.get("testCode");
    }
});

The result is: 1,2,3,4.

pm.test("Testing 2",function()
{
    for(var i=0;i<4;i++)
    {
        console.log(pm.globals.get("testCode");
    }
});

O Resultado é 4,4,4,4 e não 1,2,3,4.

  • It’s just that I don’t really remember right now, but it seems to me that when you call a for asynchronously it will always return the last processed value.

  • Daniel, note that in the first test you modify the global 4 times testCode. The value that will remain on it is the last assigned, which is 4. Therefore, in the second test, since you are no longer making any assignment to testCode, the value of it remains always 4. Anyway, it wasn’t very clear what your goal was in the second test. Why would you like its output to be the same as the first test?

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.