# Pre-request script and Tests: Need help in resolving 'undefined' error

**URL:** <https://community.postman.com/t/pre-request-script-and-tests-need-help-in-resolving-undefined-error/17255>\
**Category:** Help Hub\
**Tags:** variables\
**Created:** [October 30, 2020, 1:43am UTC](https://community.postman.com/t/pre-request-script-and-tests-need-help-in-resolving-undefined-error/17255 "2020-10-30T01:43:36Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![snk\_qa](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/snk_qa/32/6492_2.png) [@snk\_qa](https://community.postman.com/u/snk_qa)\
**Post date:** [October 30, 2020, 1:43am UTC](https://community.postman.com/t/pre-request-script-and-tests-need-help-in-resolving-undefined-error/17255/1 "2020-10-30T01:43:36Z")

</div>

I have written pre-request script and storing the response in a variable.  
_pm.sendRequest(“[http://localhost:8086/query?db=edgeOpsApp&q=SHOW](http://localhost:8086/query?db=edgeOpsApp&q=SHOW) MEASUREMENTS”, function (err, response) {_

- resp\_tst=;\*
- \_.each(response.json().results[0].series[0].values, (item) =\> {resp\_tst.push(item[0])})\*  
_});_

resp\_tst is the variable.

Now, in the Test tab, I want to print this variable.

_console.log(resp\_tst)_

This was working for me all the time, but from past few hours I getting ‘undefined’ in the response.  
Not sure what is the problem. I have not changed any settings/configurations.

Basically, variable from pre-request is not working in Tests tab  
Please help!

---

<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:** [October 30, 2020, 2:21am UTC](https://community.postman.com/t/pre-request-script-and-tests-need-help-in-resolving-undefined-error/17255/2 "2020-10-30T02:21:24Z")

</div>

@snk_qa: Welcome to the community 👋

Can you please share the response if possible? May be the schema got changed in your response. The script is unable to pick the variable then.

---

<div class="post-metadata">

**Author:** ![snk\_qa](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/snk_qa/32/6492_2.png) [@snk\_qa](https://community.postman.com/u/snk_qa)\
**Post date:** [October 30, 2020, 2:30am UTC](https://community.postman.com/t/pre-request-script-and-tests-need-help-in-resolving-undefined-error/17255/3 "2020-10-30T02:30:13Z")

</div>

![image](https://us1.discourse-cdn.com/getpostman/original/3X/a/0/a02733ca6db77fa7e3075844006ff9f67434768f.png)

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/9/6/962dca4b9cd1b6cfab199da31a83e27cd5e494c1.png)

when I print the response in Pre-request Tab, the value is correctly displayed in console. But same is not getting accessed in Tests tab.

---

<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:** [October 30, 2020, 11:37am UTC](https://community.postman.com/t/pre-request-script-and-tests-need-help-in-resolving-undefined-error/17255/4 "2020-10-30T11:37:52Z")

</div>

If you want your variable to persist from the pre-request script to the test script, you need to save it as a postman variable.

```auto
pm.variables.set('resp_tst', JSON.stringify(resp_tst));

```

In your test script you would just pull it out of postman:

```auto
const resp_tst = JSON.parse(pm.variables.get('resp_tst'));

```

---

<div class="post-metadata">

**Author:** ![snk\_qa](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/snk_qa/32/6492_2.png) [@snk\_qa](https://community.postman.com/u/snk_qa)\
**Post date:** [October 30, 2020, 2:05pm UTC](https://community.postman.com/t/pre-request-script-and-tests-need-help-in-resolving-undefined-error/17255/5 "2020-10-30T14:05:02Z")

</div>

Thanks Allen. This works.

But I wonder how did it work before. I’m running this tests from 2 months now without setting the variables. From yesterday it is suddenly failing.

Thanks for your help.
