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

**URL:** <https://community.postman.com/t/pm-variables-get-returns-undefined-after-using-set/56503>\
**Category:** Help Hub\
**Tags:** variables, scripts\
**Created:** [January 12, 2024, 9:38am UTC](https://community.postman.com/t/pm-variables-get-returns-undefined-after-using-set/56503 "2024-01-12T09:38:07Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![descent-module-ast11](https://avatars.discourse-cdn.com/v4/letter/d/b782af/32.png) [@descent-module-ast11](https://community.postman.com/u/descent-module-ast11)\
**Post date:** [January 12, 2024, 9:38am UTC](https://community.postman.com/t/pm-variables-get-returns-undefined-after-using-set/56503/1 "2024-01-12T09:38:07Z")

</div>

Test \> API 1

```auto
//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

```auto
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

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [January 12, 2024, 9:42am UTC](https://community.postman.com/t/pm-variables-get-returns-undefined-after-using-set/56503/2 "2024-01-12T09:42:42Z")

</div>

Hey @descent-module-ast11

Welcome to the Postman Community! :postman:

> [@descent-module-ast11](#):
>
> `pm.variables.set`

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](https://learning.postman.com/docs/sending-requests/troubleshooting-api-requests/). This will show you exactly what’s the value of something is at any given time.

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

```

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/system/32/29796_2.png) [@system](https://community.postman.com/u/system)\
**Post date:** [January 15, 2024, 9:43am UTC](https://community.postman.com/t/pm-variables-get-returns-undefined-after-using-set/56503/3 "2024-01-15T09:43:28Z")

</div>

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