GET response brings back a certain number of elements.
Have an array, sized with expected number of elements anticipated.
So we walk through the GET response (for-loop), checking each element in the GET response for specific data depending on data in that element of the response (if field1=āxyā then check this value and that value) and putting the ākeyā value (field1) into the array allocated earlier.
Then want to check the array to ensure the keys received in the GET response were the expected onesā¦and thereās the issue:
I have an array of size 2, and first array element has a value and second if undefined because the related event didnāt come in the GET response.
Q: How can you check an array element for an āundefinedā value?
let myVar1;
if(myVar1 === undefined){
console.log("myVar1 is undefined")
}else{
console.log("myVar1 is defined")
}
//------------------------------------
let myVar2 = 1;
if(myVar2 === undefined){
console.log("myVar2 is undefined")
}else{
console.log("myVar2 is defined")
}
pm.test("myVar1 is defined", function () {
pm.expect(myVar1).to.not.be.a("undefined");
});
pm.test("myVar2 is defined", function () {
pm.expect(myVar2).to.not.be.a("undefined");
});
Iām working with an array here, so it doesnāt like that syntaxā¦
pm.expect(eventFound[i].to.not.be.a(āundefinedā));
gives: TypeError: Cannot read properties of undefined (reading ābeā)
can recreate it (canāt share the real thing:, see below:
an array is created:
var eventFound = new Array(2);
And filled by a for loop, processing each received event, but not all slots in the array will be filled if the number of expected events are not received (hence the undefined entries being left).
for(var i = 0; i < 2; i++) {
eventFound[i] = āSomeEventNameā;
Then we loop through that same array, looking for undefined entries (if any):
for(var i = 0; i < eventFound.length; i++) {
pm.expect((eventFound[i]).to.not.be.a(āundefinedā)); //not workingā¦
Itās hard to work it out without seeing the actual code etc.
But assuming your array values are strings then try this;
let count = 0;
for (i = 0; i < myArray.length; i++) {
//Tests to PASS
try {
pm.expect(myArray[i]).to.not.eql("undefined");
}
catch (e) {
pm.test("Check if index " + [i] + " is undefined", () => {
throw new Error("Value is undefined")
}), count++;
}
}
Thanks much, and sorry I couldnāt share the real codeā¦
The earlier suggestion: pm.expect(eventFound[i]).to.not.be.a("undefined","undefined event found");
actually does work, I had an IF catching it above and calling pm.expect.fail so it wasnāt getting thereā¦
Iām sure the try/catch would probaby work as well. Thanks again!