TypeError: Cannot read property 'id' of null

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

}

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.