# PM Sendrequest return value to collectionvariable

**URL:** <https://community.postman.com/t/pm-sendrequest-return-value-to-collectionvariable/18835>\
**Category:** Help Hub\
**Tags:** tests, variables, json\
**Created:** [January 4, 2021, 4:33pm UTC](https://community.postman.com/t/pm-sendrequest-return-value-to-collectionvariable/18835 "2021-01-04T16:33:04Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![pr85](https://avatars.discourse-cdn.com/v4/letter/p/a183cd/32.png) [@pr85](https://community.postman.com/u/pr85)\
**Post date:** [January 4, 2021, 4:33pm UTC](https://community.postman.com/t/pm-sendrequest-return-value-to-collectionvariable/18835/1 "2021-01-04T16:33:04Z")

</div>

So this is a bit of a sticky question, but here goes. I’m trying to collect results so that I can aggregate arrays within a single call. Here’s the code that is problematic

```
_.each(returnValue, function(item, i) {
    pm.sendRequest(
        {
        url: 'XXXXXX',
        method: "GET",
        header: {
        "content-type": "application/json",
        "accept": "application/json",
        },
        body: {
            
        }

        }, function (err, serialresponse) {
            pm.collectionVariables.set('serial', JSON.stringify(serialresponse.json())); 
        });
});

```

Here’s the issue, I need to append to the collection variable, but this code simply overwrites it for each iteration of the loop (likely expected). I wondered if there was a way to use an array like function so that instead of overwriting collection variables, it would simply add to them, and when the loop is finished, I would have a complete array of all the return values.

P.

---

<div class="post-metadata">

**Author:** ![bpricilla](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/bpricilla/32/6511_2.png) [@bpricilla](https://community.postman.com/u/bpricilla)\
**Post date:** [January 5, 2021, 1:32am UTC](https://community.postman.com/t/pm-sendrequest-return-value-to-collectionvariable/18835/2 "2021-01-05T01:32:38Z")

</div>

Hello @pr85, I believe it should be possible and the same sort of query is answered in the link below:

> [@Saving environment variables in an array](https://community.postman.com/t/saving-environment-variables-in-an-array/18479/2):
>
> With the current JSON response provided, the below snippet could work. var jsonData = JSON.parse(responseBody); console.log(jsonData[0].site); pm.environment.set("site1", jsonData[0].site); siteArray = []; for (var i=0; i\<jsonData.length; i++) { var site = jsonData[i].site; //console.log(jsonData[i].site) siteArray.push(`${site}`); } console.log(siteArray); [image]

May be you just need to tweak a bit since you are storing the values from sendrequest and the variable scope is at collection level.

Hope this helps 😊

---

<div class="post-metadata">

**Author:** ![allenheltondev](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/allenheltondev/32/31738_2.png) [@allenheltondev](https://community.postman.com/u/allenheltondev)\
**Post date:** [January 5, 2021, 12:42pm UTC](https://community.postman.com/t/pm-sendrequest-return-value-to-collectionvariable/18835/3 "2021-01-05T12:42:26Z")

</div>

If you want to aggregate the responses over time, you’re almost there. I would do the array manipulation outside of your forEach loop to do something like this:

```auto
let serial = pm.collectionVariables.has('serial') ? JSON.parse(pm.collectionVariables.get('serial') : [];
_.each(returnValue, function(item, i) {
    pm.sendRequest(
        {
        url: 'XXXXXX',
        method: "GET",
        header: {
        "content-type": "application/json",
        "accept": "application/json",
        },
        body: {
            
        }

        }, function (err, serialresponse) {
            serial.push(serialresponse.json());
        });
});

pm.collectionVariables.set('serial', JSON.stringify(serial));

```

This will add the response to the `serial` array every time it is run.

---

<div class="post-metadata">

**Author:** ![pr85](https://avatars.discourse-cdn.com/v4/letter/p/a183cd/32.png) [@pr85](https://community.postman.com/u/pr85)\
**Post date:** [January 5, 2021, 1:41pm UTC](https://community.postman.com/t/pm-sendrequest-return-value-to-collectionvariable/18835/4 "2021-01-05T13:41:17Z")

</div>

> [@allenheltondev](#):
>
> `pm.collectionVariables.set('serial', JSON.stringify(serial));`

One would think that this would work but it doesn’t. The problem is that the loop doesn’t finish before the variable is called (due to the asynchronous nature of javascript). So the collectionvariable is null as the calls haven’t been made.

P.

---

<div class="post-metadata">

**Author:** ![allenheltondev](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/allenheltondev/32/31738_2.png) [@allenheltondev](https://community.postman.com/u/allenheltondev)\
**Post date:** [January 5, 2021, 1:59pm UTC](https://community.postman.com/t/pm-sendrequest-return-value-to-collectionvariable/18835/5 "2021-01-05T13:59:51Z")

</div>

Then maybe your best bet is to move all of this to a proper postman request and loop over it that way.
