My question:
I am trying to access a local variable (pm.variables) from within an anonymous function which handles a sendRequest call. It returns undefined when in the handler of the sendRequest but returns the correct value from within a pm.test(…) anonymous function.
I first thought that this might be more of a JS problem but then I found out that the variable seems to be there but I cannot query it. In the example below I log pm.variables twice with different results.
What I would like to do:
Access pm.variables.get(“tmpSeriesKey”) from within the anonymous function handling the sendRequest.
Details (like screenshots):
pm.test("Create series returns correct response code", function () {
pm.expect(pm.response.code).to.be.oneOf([201, 202]);
});
pm.test("Series can be found", function () {
// Check if the resource can be read and equals the data sent.
console.log(pm.variables);
pm.sendRequest(pm.environment.get("baseUrl") + '/series/' + pm.variables.get("tmpSeriesKey") + '/', function (err, response) {
//console.log(response.json());
console.log(pm.variables);
var data = response.json();
pm.expect(response.code).to.be.eql(200);
});
});
// Delete temporary series
pm.sendRequest(
{
url: pm.environment.get("baseUrl") + '/series/' + pm.variables.get("tmpSeriesKey") + '/',
method: 'DELETE'
},
function (err, response) {
if(response.code != 204)
console.warn("Error while cleaning up series '" + pm.variables.get("tmpSeriesKey") + "'. Expected delete to return 204.");
}
);
pm.variables.unset("tmpSeriesKey");
How I found the problem:
I need to validate some values from a fetched object against local variables (set in pre-request-script)
I’ve already tried:
Various tweaks with JS arrow functions and referencing parent scope variables.