Pm code checking array for undefined element

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?

Hi @ksully

Maybe this would help?

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")
}

Output;
image

very cool, can you tell me how to convert that to a pm.expect statement?
this doesnā€™t work, etc. pm.expect(eventFound[i] != undefined);

You could do something like this;

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");
});

Output;

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ā€™)

Iā€™d need a screenshot or something, there isnā€™t enough information to work with thereā€¦

Could you show your test tab code and response?

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++;
  }
}

image

1 Like

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!

1 Like