I'm trying to pass a variable from JSON with Postman to an environment variable.
The variable is successfully extracted, but I can't seem to save it into the environment variable. My code
var allGazette = JSON.parse(responseBody);
if(allGazette.total_count!==0){
for (i = 0; i < allGazette.total_count; i++) {
var dateUse=allGazette.items[i].date;
console.log(dateUse);
postman.setEnvironmentVariable('jsonGazetteDate', dateUse);
console.log(jsonGazetteDate);
}
}
else(postman.setEnvironmentVariable('jsonGazetteDate',''));
My error is at the line starting postman.set... As per console logs below, dateUse successfully comes out as 2018-05-01. I've tried various combinations of stringify/parse but can't get this to work. Any ideas?
GET https://api.companieshouse.gov.uk...
2018-05-01
ReferenceError | jsonGazetteDate is not defined
GetGazette: ReferenceError: jsonGazetteDate is not defined
It looks like the jsonGazetteDate variable is not declared but you're trying to log it to the console. So it's more than likely erroring while looping through the data.
If you're trying to log out the environment variable that you set in the line before, you'd need to do this:
console.log(pm.environment.get('jsonGazetteDate'))
Also, I would swap out those older postman syntax statements in favor of the newer pm.* functions, if you're using the native client app.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With