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

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

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.

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.

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

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

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:

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”.

console.log(jsonData.data.endOfTermReason);

Therefore, this is failing.

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

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

{
    "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": {}
    }
}

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.

Hi, I added a console log.

In postman console it is showed as “1”

but in newman it shows as:

  1â „ TypeError in test-script

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

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

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.
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?

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

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