Pm.variables.get() returns undefined after using .set()

Test > API 1

//environment vars
const responseData = pm.response.json();
pm.environment.set('PostID', responseData.id);
pm.environment.set("PostID_Timestamp", responseData.createdAt);
requestData = pm.request.body.toJSON();
pm.variables.set("createdBy",JSON.stringify(requestData.createdBy));
pm.variables.set("message",JSON.stringify(requestData.message));

Test > API 2

pm.test('Verify post details data with the create post request', function(){
const actual_input=pm.variables.get('createdBy');
const expected_input =  getResponse.item[0].createdBy;
pm.expect(actual_input).to.be.eql(expected_input); 
});

Assertion fail message : AssertionError:expected undefined to deeply equal ‘c9684795-49b1…’

Please help to fix the above error

Hey @descent-module-ast11

Welcome to the Postman Community! :postman:

This scope of variable doesn’t store anything outside of the execution of that request. That’s the reason why you wouldn’t see anything when using pm.variables.get() in the second request.

You would need to change the scope to something else (Global/Collection/Environment) that would store the value and make it reusable.

When debugging, ensure that you’re making use of the Postman Console. This will show you exactly what’s the value of something is at any given time.

console.log(pm.variables.get('createdBy'));

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