# Pm code checking array for undefined element

**URL:** <https://community.postman.com/t/pm-code-checking-array-for-undefined-element/36315>\
**Category:** Help Hub\
**Tags:** array\
**Created:** [June 17, 2022, 7:15pm UTC](https://community.postman.com/t/pm-code-checking-array-for-undefined-element/36315 "2022-06-17T19:15:26Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![ksully](https://avatars.discourse-cdn.com/v4/letter/k/b487fb/32.png) [@ksully](https://community.postman.com/u/ksully)\
**Post date:** [June 17, 2022, 7:15pm UTC](https://community.postman.com/t/pm-code-checking-array-for-undefined-element/36315/1 "2022-06-17T19:15:26Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![w4dd325](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/w4dd325/32/12436_2.png) [@w4dd325](https://community.postman.com/u/w4dd325)\
**Post date:** [June 19, 2022, 10:18pm UTC](https://community.postman.com/t/pm-code-checking-array-for-undefined-element/36315/2 "2022-06-19T22:18:45Z")

</div>

Hi @ksully

Maybe this would help?

```auto
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](https://us1.discourse-cdn.com/getpostman/original/2X/1/13d2eb81087f7cd6eba7af93feebdf01fe1d3361.png)

---

<div class="post-metadata">

**Author:** ![ksully](https://avatars.discourse-cdn.com/v4/letter/k/b487fb/32.png) [@ksully](https://community.postman.com/u/ksully)\
**Post date:** [June 20, 2022, 1:18pm UTC](https://community.postman.com/t/pm-code-checking-array-for-undefined-element/36315/3 "2022-06-20T13:18:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![w4dd325](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/w4dd325/32/12436_2.png) [@w4dd325](https://community.postman.com/u/w4dd325)\
**Post date:** [June 20, 2022, 4:23pm UTC](https://community.postman.com/t/pm-code-checking-array-for-undefined-element/36315/4 "2022-06-20T16:23:29Z")

</div>

You could do something like this;

```auto
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;

 ![image](https://us1.discourse-cdn.com/getpostman/original/2X/6/68de6938ac2b15035237d925c699d4a708539ca9.png)

---

<div class="post-metadata">

**Author:** ![ksully](https://avatars.discourse-cdn.com/v4/letter/k/b487fb/32.png) [@ksully](https://community.postman.com/u/ksully)\
**Post date:** [June 20, 2022, 4:45pm UTC](https://community.postman.com/t/pm-code-checking-array-for-undefined-element/36315/5 "2022-06-20T16:45:35Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![w4dd325](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/w4dd325/32/12436_2.png) [@w4dd325](https://community.postman.com/u/w4dd325)\
**Post date:** [June 20, 2022, 4:49pm UTC](https://community.postman.com/t/pm-code-checking-array-for-undefined-element/36315/6 "2022-06-20T16:49:56Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![ksully](https://avatars.discourse-cdn.com/v4/letter/k/b487fb/32.png) [@ksully](https://community.postman.com/u/ksully)\
**Post date:** [June 20, 2022, 5:16pm UTC](https://community.postman.com/t/pm-code-checking-array-for-undefined-element/36315/7 "2022-06-20T17:16:07Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![w4dd325](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/w4dd325/32/12436_2.png) [@w4dd325](https://community.postman.com/u/w4dd325)\
**Post date:** [June 20, 2022, 5:48pm UTC](https://community.postman.com/t/pm-code-checking-array-for-undefined-element/36315/8 "2022-06-20T17:48:35Z")

</div>

It’s hard to work it out without seeing the actual code etc.

But assuming your array values are strings then try this;

```auto
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](https://us1.discourse-cdn.com/getpostman/original/2X/a/a7c1a63d2bf7b3d9a70fb5fe0a7e5294a345cfd0.png)

---

<div class="post-metadata">

**Author:** ![ksully](https://avatars.discourse-cdn.com/v4/letter/k/b487fb/32.png) [@ksully](https://community.postman.com/u/ksully)\
**Post date:** [June 20, 2022, 6:22pm UTC](https://community.postman.com/t/pm-code-checking-array-for-undefined-element/36315/9 "2022-06-20T18:22:17Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/system/32/29796_2.png) [@system](https://community.postman.com/u/system)\
**Post date:** [December 12, 2024, 12:54pm UTC](https://community.postman.com/t/pm-code-checking-array-for-undefined-element/36315/10 "2024-12-12T12:54:13Z")

</div>

Closed on December 12, 2024, 12:54pm UTC

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