0

OK. I have been reading the Postman docs and looking at various posted examples but I cannot seem to be able to have the Postman web or windows app (v8.8.0) utilize the .set functions effectively to establish and update variables as I always get the following:

ReferenceError: [variablename] is not defined" 

Here are my simple test prerequest script and variables defined that I have tried at different scopes in prerequest scripts:

Example 1) Local Variable

var random = Math.floor(Math.random() * 10);
pm.variables.set("randomVal",random);
console.log(randomVal);

Result: ReferenceError: randomVal is not defined

Expected: {random > number up to 10}

Example 2) Environment Variable

pm.environment.set("enviro_variable_key", "enviro_variable_value");
pm.environment.get("enviro_variable_key");
console.log(enviro_variable_key);

ReferenceError: enviro_variable_key is not defined

Expected: enviro_variable_value

Example 3)

pm.globals.set("global_variable_key", "global_variable_value");
pm.globals.get("global_variable_key");
console.log(global_variable_key);

Result: ReferenceError: global_variable_key is not defined

Expected: global_variable_value

Example 4)

pm.collectionVariables.set("collection_var_key", "collection_var_value")
pm.collectionVariables.get("collection_var_key");
console.log(collection_var_key);

Result: ReferenceError: collection_var_key is not defined

Expected: collection_var_value


Any clarification or help - or see if these simple prerequest scripts work on your system - would be appreciated

1 Answers1

0

You're creating a scoped variable and then trying to read a local variable, which isn't defined.

The syntax you need would be this:

pm.collectionVariables.set('collection_var_key', 'some_collection_variable');
console.log(pm.collectionVariables.get('collection_var_key');
Danny Dainton
  • 23,069
  • 6
  • 67
  • 80