I have body response:
“persons”: [
{
“id”: 377,
“forename”: “Forename 493068”,
“surname”: “Surname 493068”,
“dateOfBirth”: “1971-06-16”
},
{
“id”: 376,
“forename”: “Forename 493067”,
“surname”: “Surname 493067”,
“dateOfBirth”: “1971-01-14”
},
Here’s an outline with best practices for making your inquiry.
I check if dateOfBirth is null or string:
const jsonData = pm.response.json();
pm.test(“Test work with loops persons”, () => {
console.log(jsonData.persons.length);
pm.expect(jsonData.persons).to.be.an(‘array’).that.is.not.empty;
for(let i = 0; i < jsonData.value.persons.length; i++)
var expiryDate = jsonData.accounts[i].dateOfBirth;
pm.expect(dateOfBirth).to.be.oneOf([null,“string”]);
I
My question :
TypeError: Cannot read properties of undefined (reading ‘null’)
How to fix this check?
w4dd325
(w4dd325)
September 24, 2022, 3:51am
2
Hi @cryosat-observer-223
I think you are using the wrong variable name.
var expiryDate = jsonData.accounts[i].dateOfBirth;
pm.expect(dateOfBirth).to.be.oneOf([null,“string”]);
Try changing it to;
var expiryDate = jsonData.accounts[i].dateOfBirth;
pm.expect(expiryDate).to.be.oneOf([null,“string”]);
yep, I detected wrong var name later, thanks
But now I see the next error:
AssertionError: expected ‘1971-06-16’ to be one of [ null, ‘string’ ]
Please consider the following…
jsonData =
[{
"id": 377,
"forename": "Forename 493068",
"surname": "Surname 493068",
"dateOfBirth": "1971-06-16"
},
{
"id": 376,
"forename": "Forename 493067",
"surname": "Surname 493067",
"dateOfBirth": "1971-01-14"
},
{
"id": 375,
"forename": "Forename 123456",
"surname": "Surname 123456",
"dateOfBirth": null
}];
console.log(jsonData.length);
pm.test("Array check", () => {
pm.expect(jsonData).to.be.an("array").that.is.not.empty;
});
for (let i = 0; i < jsonData.length; i++) {
var expiryDate = jsonData[i].dateOfBirth;
console.log(expiryDate);
pm.test("String?", () => {
pm.expect(expiryDate).to.be.an("string");
});
// oneOf checks values not types, so this will fail.
pm.test("Expiry date", () => {
pm.expect(expiryDate).to.be.oneOf([null,"string"]);
});
// This will work as long as its a string
pm.test("Expiry date v2", () => {
pm.expect(typeof(expiryDate)).to.be.oneOf(["string", null]);
// null returns the type as an object
});
};
You probably need a function to help with this.
jsonData =
[{
"id": 377,
"forename": "Forename 493068",
"surname": "Surname 493068",
"dateOfBirth": "1971-06-16"
},
{
"id": 376,
"forename": "Forename 493067",
"surname": "Surname 493067",
"dateOfBirth": "1971-01-14"
},
{
"id": 375,
"forename": "Forename 123456",
"surname": "Surname 123456",
"dateOfBirth": null
}];
console.log(jsonData.length);
// helper function to deal with null
function getType(value) {
if (value === null || value === undefined) return null
const type = typeof(value)
if (type == 'object') {
if (value instanceof Array) return 'array'
if (value instanceof Date) return 'date'
}
return type
};
pm.test("Array check", () => {
pm.expect(jsonData).to.be.an("array").that.is.not.empty;
});
for (let i = 0; i < jsonData.length; i++) {
var expiryDate = jsonData[i].dateOfBirth;
console.log(expiryDate);
// Using the helper function
pm.test("Expiry date v3", () => {
pm.expect(getType(expiryDate)).to.be.oneOf(["string", null]);
});
};
As this is an array, you can also use a forEach loop.
jsonData =
[{
"id": 377,
"forename": "Forename 493068",
"surname": "Surname 493068",
"dateOfBirth": "1971-06-16"
},
{
"id": 376,
"forename": "Forename 493067",
"surname": "Surname 493067",
"dateOfBirth": "1971-01-14"
},
{
"id": 375,
"forename": "Forename 123456",
"surname": "Surname 123456",
"dateOfBirth": null
}];
console.log(jsonData.length);
// helper function to deal with null
function getType(value) {
if (value === null || value === undefined) return null
const type = typeof(value)
if (type == 'object') {
if (value instanceof Array) return 'array'
if (value instanceof Date) return 'date'
}
return type
};
pm.test("Array check", () => {
pm.expect(jsonData).to.be.an("array").that.is.not.empty;
});
jsonData.forEach(element => {
var expiryDate = element.dateOfBirth;
console.log(expiryDate);
// Using the helper function
pm.test("Expiry date v3", () => {
pm.expect(getType(expiryDate)).to.be.oneOf(["string", null]);
});
});
This helped, Thanks - typeof - the solution for my case