# TypeError Cannot read properties of undefined (reading 'id') at test-script

**URL:** <https://community.postman.com/t/typeerror-cannot-read-properties-of-undefined-reading-id-at-test-script/46198>\
**Category:** Help Hub\
**Tags:** newman-project\
**Created:** [April 11, 2023, 10:05am UTC](https://community.postman.com/t/typeerror-cannot-read-properties-of-undefined-reading-id-at-test-script/46198 "2023-04-11T10:05:44Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![geneta](https://avatars.discourse-cdn.com/v4/letter/g/ed655f/32.png) [@geneta](https://community.postman.com/u/geneta)\
**Post date:** [April 11, 2023, 10:05am UTC](https://community.postman.com/t/typeerror-cannot-read-properties-of-undefined-reading-id-at-test-script/46198/1 "2023-04-11T10:05:45Z")

</div>

> **Archived topic — this may be out of date.**  
> Newman has been replaced by the **[Postman CLI](https://learning.postman.com/docs/postman-cli/postman-cli-overview/)**, Postman’s official, signed command-line tool. It runs collections as Newman did, and also lints API specs, runs mocks and monitors, and syncs local files with Postman from your terminal, CI/CD pipeline or coding agent.  
> Working with an AI coding agent? The [Postman plugin](https://github.com/postmanlabs/postman-plugin) lets agents in Claude Code, Cursor, Codex and others design, mock, test and ship APIs with Postman.  
> For current help, see the [Postman docs](https://learning.postman.com/docs/), try the free [Postman tools](https://www.postman.com/tools) (no account needed), or join us on [Discord](https://discord.gg/58aNNsRBGR).

Hi, can you please help me to resolve this issue on newman. This is the error that i’m getting in newman: `TypeError: Cannot read properties of undefined (reading 'id') at test-script`

compared to postman runner, I do not encounter this issue, only when running to newman.

This is the postman tests that I created

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

pm.collectionVariables.set("vesselAccountingId", jsonData.data.id);
pm.collectionVariables.set("vesselName", jsonData.data.vesselName);

pm.test("C28806 Status code is 200", function () {
    pm.response.to.have.status(200);
});

pm.test("C28806 Response time is less than 2000ms", function () {
    pm.expect(pm.response.responseTime).to.be.below(2000);
});

pm.test("C28806	Response data for endOfTermReason=0", function () {
    pm.expect(response.data.endOfTermReason).to.eql(0);
});

console.log(responseBody);

```

I already added CONST and VAR but nothing works.  
I’m using the same test on my other tests but this issue only occurs in this post request  
I’ve already tried to update newman to latest version, still not working.  
Tried to change the variabl from collection to environment, still not working.

Hope anyone can help with this.

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [April 11, 2023, 11:39am UTC](https://community.postman.com/t/typeerror-cannot-read-properties-of-undefined-reading-id-at-test-script/46198/2 "2023-04-11T11:39:22Z")

</div>

> [@geneta](#):
>
> `jsonData.data.id`

console log this before you try and set the collectionVariable.

This is the only place I can see “id” in your code, so I’m assuming this is the problematic line.

```auto
const jsonData = pm.response.json();
console.log(jsonData.data.id);
pm.collectionVariables.set("vesselAccountingId", jsonData.data.id);
console.log(pm.collectionVariables.get("vesselAccountingId");

```

---

<div class="post-metadata">

**Author:** ![geneta](https://avatars.discourse-cdn.com/v4/letter/g/ed655f/32.png) [@geneta](https://community.postman.com/u/geneta)\
**Post date:** [April 11, 2023, 12:25pm UTC](https://community.postman.com/t/typeerror-cannot-read-properties-of-undefined-reading-id-at-test-script/46198/3 "2023-04-11T12:25:09Z")

</div>

now the error do not show in post request anymore, but in put request i can still encounter the error, this is my tests

```auto
const jsonData = pm.response.json();
console.log(jsonData.data.endOfTermReason);
pm.collectionVariables.set("endOfTermReason", jsonData.data.endOfTermReason);
console.log(pm.collectionVariables.get("endOfTermReason"));

pm.test("C28806 Status code is 200", function () {
    pm.response.to.have.status(200);
});

pm.test("C28806 Response time is less than 2000ms", function () {
    pm.expect(pm.response.responseTime).to.be.below(2000);
});

pm.test("C28803 Response data for endOfTermReason not 0", function () {
    pm.expect(jsonData.data.endOfTermReason).to.not.eql(0);
});

```

result in newman:

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/7/a/7a2f4b68e45b8ad8a8e89d8f2e7f23a7d06e9193.png)

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [April 11, 2023, 3:40pm UTC](https://community.postman.com/t/typeerror-cannot-read-properties-of-undefined-reading-id-at-test-script/46198/4 "2023-04-11T15:40:47Z")

</div>

Can you please include a sample response. Have you also checked the console logs to see what is being returned?

I’m guessing that the following is returning “undefined”.

```auto
console.log(jsonData.data.endOfTermReason);

```

Therefore, this is failing.

```auto
pm.collectionVariables.set("endOfTermReason", jsonData.data.endOfTermReason);
console.log(pm.collectionVariables.get("endOfTermReason"));

```

Just a guess though without seeing an full example request\response.

When you have errors like this, build the tests back up line by line, so you can immediately start to see when it fails (and which line is failing).

---

<div class="post-metadata">

**Author:** ![geneta](https://avatars.discourse-cdn.com/v4/letter/g/ed655f/32.png) [@geneta](https://community.postman.com/u/geneta)\
**Post date:** [April 12, 2023, 10:25am UTC](https://community.postman.com/t/typeerror-cannot-read-properties-of-undefined-reading-id-at-test-script/46198/5 "2023-04-12T10:25:47Z")

</div>

> [@michaelderekjones](#):
>
> ```auto
> pm.collectionVariables.set("endOfTermReason", jsonData.data.endOfTermReason);
> console.log(pm.collectionVariables.get("endOfTermReason"));
> 
> ```

Hellooooo, please see below the sample response from postman. Dont know why it is still failing in newman

```auto
{
    "data": {
        "endOfTermReason": 1,
        "vesselNo": 5174,
        "vesselName": "Testing Haag Inc",
        "vesselId": 254,
        "from": "2023-04-15T08:34:00Z",
        "until": "2023-04-20T08:34:00Z",
        "id": 110,
        "_type": "VesselAccounting"
    },
    "meta": {
        "redirect": {}
    }
}

```

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [April 12, 2023, 12:40pm UTC](https://community.postman.com/t/typeerror-cannot-read-properties-of-undefined-reading-id-at-test-script/46198/6 "2023-04-12T12:40:53Z")

</div>

What is the console logs showing? Is it “undefined” or 1?

Based on your example response, the code looks fine to me so the next steps are checking the log files.

First in Postman, then in Newman.

Are you sure the response is coming back ok in Newman first? Console log the entire response as the first troubleshooting step. (jsonData in your example). Do it at the top just after you define the response before you do anything else.

---

<div class="post-metadata">

**Author:** ![geneta](https://avatars.discourse-cdn.com/v4/letter/g/ed655f/32.png) [@geneta](https://community.postman.com/u/geneta)\
**Post date:** [April 14, 2023, 9:56am UTC](https://community.postman.com/t/typeerror-cannot-read-properties-of-undefined-reading-id-at-test-script/46198/7 "2023-04-14T09:56:05Z")

</div>

Hi, I added a console log.

In postman console it is showed as “1”

but in newman it shows as:

```auto
  1⠄ TypeError in test-script

```

This is what I added in the tests as per your instruction if its correct:

```auto
const jsonData = pm.response.json();
console.log(jsonData.data.endOfTermReason);

```

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [April 14, 2023, 10:08am UTC](https://community.postman.com/t/typeerror-cannot-read-properties-of-undefined-reading-id-at-test-script/46198/8 "2023-04-14T10:08:27Z")

</div>

Can you console log the entire response as the first step, and ensure that the response is returning properly as I can’t see a problem on face value with your targeting of the endOfTermReason element.

I would also recommend removing all other code from the tests tab, and build it back up line by line, so you can work out exactly which line is failing (which means removing the tests to start with and just having a bunch of console logs before adding the tests back in one by one).

- Parse your response
- Console log your response.
- Console log the endOfTermReason element.

```auto
const jsonData = pm.response.json();
console.log(jsonData);
console.log(jsonData.data.endOfTermReason);

```

No setting of collection variables at this point. See how that goes. Does this at least work?

---

<div class="post-metadata">

**Author:** ![geneta](https://avatars.discourse-cdn.com/v4/letter/g/ed655f/32.png) [@geneta](https://community.postman.com/u/geneta)\
**Post date:** [April 24, 2023, 5:50am UTC](https://community.postman.com/t/typeerror-cannot-read-properties-of-undefined-reading-id-at-test-script/46198/9 "2023-04-24T05:50:12Z")

</div>

> [@michaelderekjones](#):
>
> ```auto
> const jsonData = pm.response.json();
> console.log(jsonData);
> console.log(jsonData.data.endOfTermReason);
> 
> ```

Hi! Your solution works! I really appreciate your help.  
Thann you very much!

---

<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:** [August 23, 2023, 3:44pm UTC](https://community.postman.com/t/typeerror-cannot-read-properties-of-undefined-reading-id-at-test-script/46198/10 "2023-08-23T15:44:24Z")

</div>

Closed on August 23, 2023, 3:44pm UTC

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