Hello guys I hope you are very well, I have the following problem. in the next object
"Customer": {
"Atention": null
}
I need to extract the value of "Atention"
in a function, but this value is dynamic and varies, but it will always have the word 'PORTFOLIO'. For this I am using the following function
function ObtenerValor () {
var data = JSON.parse(body) //Estoy omitiendo la ruta de la respuesta del JSON
var Atention1 = null;
var cadena = data.WSResponseBody.Customer.Atention; //está es la ruta del objeto `Atention` en la respuesta del JSON
var palabra = 'CARTERA';
var index = cadena.indexOf(palabra);
if(index >= 0) {
return Atention1 = "la palabra existe";
} else {
return Atention1 = "la palabra no existe dentro de la cadena";
}
}
ObtenerValor ();
And it works but when the object Atention
has a value other than null, when it comes in null, I get the following error
TypeError: Cannot read properties of null (reading 'indexOf')
I would appreciate any help :)