# What is wrong with pm.expect

**URL:** <https://community.postman.com/t/what-is-wrong-with-pm-expect/29334>\
**Category:** Help Hub\
**Created:** [November 2, 2021, 5:25pm UTC](https://community.postman.com/t/what-is-wrong-with-pm-expect/29334 "2021-11-02T17:25:20Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![hasmukhpatel](https://avatars.discourse-cdn.com/v4/letter/h/eada6e/32.png) [@hasmukhpatel](https://community.postman.com/u/hasmukhpatel)\
**Post date:** [November 2, 2021, 5:25pm UTC](https://community.postman.com/t/what-is-wrong-with-pm-expect/29334/1 "2021-11-02T17:25:20Z")

</div>

let jsonData = pm.response.json()  
for (var i=0; i\<count; i++){  
pm.test(“To check if all keys are present and value is NOT empty for eating”, function () {  
console.log(“start\_date”, jsonData.data[i].start\_date)  
console.log(“score”,jsonData.data[i].score)  
console.log(“unit”,jsonData.data[i].unit)  
console.log(“status”,jsonData.data[i].status)  
pm.expect(jsonData.data[i]).to.have.property(start\_date,score,unit,status);  
});  
}

Console is printing data but when I am saying pm.expect giving error saying  
To check if all keys are present and value is NOT empty for eating | **ReferenceError: start\_date is not defined**

Here is the response  
{  
“pet\_id”: 119517,  
“start\_date”: “2021-10-27”,  
“end\_date”: “2021-11-02”,  
“num\_of\_days”: 3,  
“score”: 0,  
“unit”: “second”,  
“status”: “not\_enough\_data”,  
“data”: [  
{  
“start\_date”: “2021-10-27”,  
“score”: 0,  
“unit”: “second”,  
“status”: “below\_average”  
},  
{  
“start\_date”: “2021-10-28”,  
“score”: 0,  
“unit”: “second”,  
“status”: “below\_average”  
},  
{  
“start\_date”: “2021-10-29”,  
“score”: 0,  
“unit”: “second”,  
“status”: “below\_average”  
}  
]  
}

---

<div class="post-metadata">

**Author:** ![skandh-15](https://avatars.discourse-cdn.com/v4/letter/s/a3d4f5/32.png) [@skandh-15](https://community.postman.com/u/skandh-15)\
**Post date:** [November 2, 2021, 5:38pm UTC](https://community.postman.com/t/what-is-wrong-with-pm-expect/29334/2 "2021-11-02T17:38:34Z")

</div>

1. Check if start\_time property is there or not?
2. If yes, then probably you need to open console and see the response that you get and check spelling.

---

<div class="post-metadata">

**Author:** ![hasmukhpatel](https://avatars.discourse-cdn.com/v4/letter/h/eada6e/32.png) [@hasmukhpatel](https://community.postman.com/u/hasmukhpatel)\
**Post date:** [November 2, 2021, 6:22pm UTC](https://community.postman.com/t/what-is-wrong-with-pm-expect/29334/3 "2021-11-02T18:22:16Z")

</div>

@skandh-15 my mistake I posted incorrect script, but yes while printing it’s printing data but error only in pm.expect.  
Still seeing same error  
To check if all keys are present and value is NOT empty for eating | **ReferenceError: start\_date is not defined**

---

<div class="post-metadata">

**Author:** ![bbahadur](https://avatars.discourse-cdn.com/v4/letter/b/9de053/32.png) [@bbahadur](https://community.postman.com/u/bbahadur)\
**Post date:** [November 3, 2021, 9:06am UTC](https://community.postman.com/t/what-is-wrong-with-pm-expect/29334/4 "2021-11-03T09:06:47Z")

</div>

Hey @hasmukhpatel!

Seems like a problem with fetching start\_date value from your json response, if you could also share the response, it will help the community better understand where the problem exists in your code.

Good luck!

---

<div class="post-metadata">

**Author:** ![hasmukhpatel](https://avatars.discourse-cdn.com/v4/letter/h/eada6e/32.png) [@hasmukhpatel](https://community.postman.com/u/hasmukhpatel)\
**Post date:** [November 3, 2021, 2:52pm UTC](https://community.postman.com/t/what-is-wrong-with-pm-expect/29334/5 "2021-11-03T14:52:14Z")

</div>

@bbahadur I added response to the original post. Thanks for looking into it.

---

<div class="post-metadata">

**Author:** ![bbahadur](https://avatars.discourse-cdn.com/v4/letter/b/9de053/32.png) [@bbahadur](https://community.postman.com/u/bbahadur)\
**Post date:** [November 3, 2021, 3:25pm UTC](https://community.postman.com/t/what-is-wrong-with-pm-expect/29334/6 "2021-11-03T15:25:40Z")

</div>

Hey @hasmukhpatel !

If you want to check if ‘data’ has all properties, you can iterate through each and perform assertion. Here is one way to achieve it:

```auto
let jsonData = pm.response.json();

console.log(jsonData.data);

pm.test("All keys present",()=>{
_.each(jsonData.data, (item)=>{
    
    pm.expect(item.hasOwnProperty('start_date')).to.be.true;
    pm.expect(item.hasOwnProperty('score')).to.be.true;
    pm.expect(item.hasOwnProperty('unit')).to.be.true;
    pm.expect(item.hasOwnProperty('status')).to.be.true;
})

})

```

Similarly you can even add assertions to check for presence of value.

Hope it helps,  
Good luck!

---

<div class="post-metadata">

**Author:** ![hasmukhpatel](https://avatars.discourse-cdn.com/v4/letter/h/eada6e/32.png) [@hasmukhpatel](https://community.postman.com/u/hasmukhpatel)\
**Post date:** [November 3, 2021, 3:37pm UTC](https://community.postman.com/t/what-is-wrong-with-pm-expect/29334/7 "2021-11-03T15:37:25Z")

</div>

> [@bbahadur](#):
>
> ```auto
> let jsonData = pm.response.json();
> 
> console.log(jsonData.data);
> 
> pm.test("All keys present",()=>{
> _.each(jsonData.data, (item)=>{
>     
> pm.expect(item.hasOwnProperty('start_date')).to.be.true;
> pm.expect(item.hasOwnProperty('score')).to.be.true;
> pm.expect(item.hasOwnProperty('unit')).to.be.true;
> pm.expect(item.hasOwnProperty('status')).to.be.true;
> })
> 
> })
> 
> ```

@bbahadur yaaaaa, it worked. Thanks Bharat for your time and guidance. I can say I can go one step ahead in postman.
