"TypeError: Cannot read property 'set' of undefined" when trying to set environment variable for JWT token in Postman

Viewed 357

Clicking the send button should create an environmental variable for the JWT token. However the postman log outputs an error:

enter image description here

enter image description here

Here is the javascript code that should handle creating the access token as an environmental variable within postman.

var jsonData = JSON.parse(responseBody);
tests["Access token was not empty"] = jsonData.access_token !== undefined;

postman.environment.set("jwt_token", jsonData.access_token);
1 Answers

New test script for postman is

var jsonData = JSON.parse(responseBody);

pm.test("Access token was not empty", () => {
   pm.expect(jsonData.access_token).not.eql(undefined);
});

pm.environment.set("jwt_token", jsonData.access_token);
Related