# TypeError: Cannot read property 'id' of null

**URL:** <https://community.postman.com/t/typeerror-cannot-read-property-id-of-null/28711>\
**Category:** Help Hub\
**Tags:** tests, collections\
**Created:** [October 13, 2021, 1:56pm UTC](https://community.postman.com/t/typeerror-cannot-read-property-id-of-null/28711 "2021-10-13T13:56:08Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![may](https://avatars.discourse-cdn.com/v4/letter/m/58f4c7/32.png) [@may](https://community.postman.com/u/may)\
**Post date:** [October 13, 2021, 1:56pm UTC](https://community.postman.com/t/typeerror-cannot-read-property-id-of-null/28711/1 "2021-10-13T13:56:08Z")

</div>

Hello guys!  
I need help with my test. I’m getting a message “_There was an error in evaluating the test script: TypeError: Cannot read property ‘id’ of null_”

This is my Test:

var jsonData = JSON.parse(responseBody);  
if (jsonData.length === 0) {  
console.log(“Pesquisa\_Router”);  
} else{  
var dict\_segmento = {};  
for (const elemento of jsonData) {  
var segmento = elemento[“segmento”];  
if (typeof segmento !== “number”){  
console.log(segmento);  
var segmentoId = segmento[“id”];  
var segmentoName = segmento[“name”];  
dict\_segmento[segmentoName] = segmentoId;  
}  
}  
console.log(dict\_segmento);

```
var nome_desejado = "Router"
var id_retorno = ""
for(var key in dict_segmento) {
    if (nome_desejado === key){
        id_retorno = dict_segmento[key];
    }
}

console.log("resultado vale "+id_retorno);
postman.setEnvironmentVariable("segmentIdRouter", id_retorno);
postman.setEnvironmentVariable("segmentNameRouter", nome_desejado);

```

}

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

---

<div class="post-metadata">

**Author:** ![kitty-garrison](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/kitty-garrison/32/27117_2.png) [@kitty-garrison](https://community.postman.com/u/kitty-garrison)\
**Post date:** [October 13, 2021, 5:21pm UTC](https://community.postman.com/t/typeerror-cannot-read-property-id-of-null/28711/2 "2021-10-13T17:21:29Z")

</div>

I don’t have the full solution yet because I don’t have a sample response body to debug with. I think these edits will get you a bit further.

1. On line 2 - `jsonData` is an object and not an array. There is not a `.length` on an object. However, you could use a method on the Object class called `.keys()` that returns the keys of the given object like this: `Object.keys(jsonData).length` to see if your object had any keys.

2. Similarly, on line 6 the `for( __of__ )` is the syntax to iterate an array. Try using `for( __in__ )` insted. Like this: `for (const elemento in jsonData){...}`

I hope that gets you a bit further. If you provide an example response body that you are testing I could perhaps provide more guidance.
