# Cannot read property 'id' of undefined

**URL:** <https://community.postman.com/t/cannot-read-property-id-of-undefined/35974>\
**Category:** Help Hub\
**Tags:** tests\
**Created:** [June 6, 2022, 10:49am UTC](https://community.postman.com/t/cannot-read-property-id-of-undefined/35974 "2022-06-06T10:49:44Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![manasasunkara27](https://avatars.discourse-cdn.com/v4/letter/m/5fc32e/32.png) [@manasasunkara27](https://community.postman.com/u/manasasunkara27)\
**Post date:** [June 6, 2022, 10:49am UTC](https://community.postman.com/t/cannot-read-property-id-of-undefined/35974/1 "2022-06-06T10:49:44Z")

</div>

**Your question may already have an answer on the community forum. Please search for related topics, and then read through the [guidelines](https://community.postman.com/t/about-the-just-getting-started-category/37) before creating a new topic.**

> Here’s an outline with best practices for making your inquiry.

_My question_: I have save response of API request as environment or global variable but it is throwing ‘Cannot read property ‘id’ of undefined’ error

_Details (like screenshots)_:

My response body :  
{  
“data”: {  
“service”: {  
“serviceTypeId”: 6,  
“id”: 565  
},  
“user”: {  
“roleId”: 1,  
“id”: 848  
},  
“service\_token”: “werh-aaaa-aaaa-bbbb-fdgrg53”,  
“customer”: {  
“id”: 752  
}  
},  
“message”: “Customer Created successfully”,  
“status”: 200  
}

My Script to store the response:  
pm.test(“Status code is 200”, function () {  
pm.response.to.have.statusCode(200);  
pm.response.to.have.status(“OK”);  
});

let jsonData = JSON.parse(responseBody);  
pm.environment.set(“service\_id”,(jsonData.data.service.id));  
pm.environmnet.set(“user\_id”,(jsonData.data.user.id));  
pm.environment.set(“customer\_id”,(jsonData.data.customer.id));

_How I found the problem_:  
Found this while testing

_I’ve already tried_: Please help me resolve the issue.  
I have given the proper authentication as I got the response 200 OK

---

<div class="post-metadata">

**Author:** ![w4dd325](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/w4dd325/32/12436_2.png) [@w4dd325](https://community.postman.com/u/w4dd325)\
**Post date:** [June 6, 2022, 6:11pm UTC](https://community.postman.com/t/cannot-read-property-id-of-undefined/35974/2 "2022-06-06T18:11:28Z")

</div>

Hi @manasasunkara27

You have a spelling mistake in your script (“environmnet”);

This will work;

```auto
const jsonData = pm.response.json();

pm.environment.set("service_id", jsonData.data.service.id);
pm.environment.set("user_id", jsonData.data.user.id);
pm.environment.set("customer_id", jsonData.data.customer.id);

```

---

<div class="post-metadata">

**Author:** ![manasasunkara27](https://avatars.discourse-cdn.com/v4/letter/m/5fc32e/32.png) [@manasasunkara27](https://community.postman.com/u/manasasunkara27)\
**Post date:** [June 7, 2022, 5:12am UTC](https://community.postman.com/t/cannot-read-property-id-of-undefined/35974/3 "2022-06-07T05:12:12Z")

</div>

> [@w4dd325](#):
>
> ```auto
> pm.environment.set("service_id", jsonData.data.service.id);
> pm.environment.set("user_id", jsonData.data.user.id);
> pm.environment.set("customer_id", jsonData.data.customer.id);
> 
> ```

My bad, thank you so much it worked
