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

I have written pre-request script and storing the response in a variable.
pm.sendRequest(“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.


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!

@snk_qa: Welcome to the community :wave:

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.

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.

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.

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

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

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

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.

1 Like