Cannot read property 'id' of undefined

Your question may already have an answer on the community forum. Please search for related topics, and then read through the guidelines 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

Hi @manasasunkara27

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

This will work;

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);

My bad, thank you so much it worked

1 Like