Getting TypeError " Cannot read properties of undefined (reading 'employee_name')"

Hi All,

I am getting the below issue once I run the collection for the multiple set of data (Users.csv).

End Point: dummy.restapiexample.com/api/v1/employees
HTTP Method: GET

Code:

pm.test(“200 status”, function() {
pm.response.to.have.status(200);

});

pm.test(“Response time”, function(){
pm.expect(pm.response.responseTime).to.be.below(4000)

});
const auth = undefined;
console.log(auth?.employee_name); // undefined
console.log(auth?.employee_name?.employee_name);

pm.test(“Verify login user”, function() {

const response = pm.response.json();

pm.expect(response.json.employee_name).to.eql(pm.iterationData.get("Emp_Name"));
pm.expect(response.json.employee_salary).to.eql(pm.iterationData.get("Emp_Salary"));
pm.expect(response.json.employee_age).to.eql(pm.iterationData.get("Emp_Age"));

});

Error:

Verify login user | TypeError: Cannot read properties of undefined (reading ‘employee_name’)

Please assist on resolving this issue.

What is is this doing?

const auth = undefined;
console.log(auth?.employee_name); // undefined
console.log(auth?.employee_name?.employee_name);

This isn’t what is causing your problem, but I can’t work out what is actually doing.

The problem is with your assertion.

pm.expect(response.employee_name).to.eql(pm.iterationData.get("Emp_Name"));

You’ve already parsed the response. You haven’t included an example response but I’m going to guess that it should be response.employee_name.

The error is reporting back as undefined as it can’t find the value.

Hi mdjones,

Thank you so much, it got resolved I have updated the assertion which you have provided. It is working now.