Setting token as environment variable

I used to set a global variable for id_token using the folowing script :slight_smile:
var jsonData = pm.response.json();

id_token =jsonData.access_token;

pm.environment.set(“id_token”, id_token);

console.log(“id_token”,id_token);

but now , it generate an error :slight_smile:
TypeError: Cannot read properties of null (reading ‘length’)

That code is not generating that error.

What code is looking at the length?

I’m guessing you have a test or string comparison somewhere else in your code.

Having a global variable with the same name as an environment variable can cause scope issues.

Variable Scopes | Postman Learning Center

You might want to consider calling the token something like “currentToken” when parsing the response, and then use this to set the environment variable.

This way, you know that you aren’t having scope issues.

I also don’t see why you would want this as a global variable.

The tokens are usually confidential information, which is why you want them in the current values in an Environment variable.

const response = pm.response.json();

let currentToken = response.access_token;

console.log("currentToken", currentToken);

pm.environment.set("id_token", currentToken);

console.log("id_token", pm.variables.get("id_token")); //or
console.log("id_token", pm.environment.get("id_token"));

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.