Control of access error to properties, read json with javascript

0

I am reading a json variable and extracting information from it.

I have the fields of the properties in an array. Everything is fine until the field I'm looking for does not exist

the application gives an error of type:

  

I have tried all the options I found on the network to control the error:

    1.var tipo = typeof PropModelo[HayarIndice(Nodos[j])].properties[nombreNivel][value]);
    if (tipo ==="undefined")  //error cuando carga la variable "tipo".
    2.if(PropModelo[HayarIndice(Nodos[j])].properties[nombreNivel][value] === void 0)//error cuando se mencionan las propiedades.

In summary I have tried all the solutions that have been presented to me by the network and it keeps giving me error whenever it is mentioned:

  

PropModelo[HayarIndice(Nodos[j])].properties[nombreNivel][value]

How can I control the error? I need that the variables that do not have this field do not break the execution of the program. Thanks.

    
asked by Clara Arias Del Rey 20.01.2017 в 13:44
source

2 answers

1

The error message is telling you that you need to check if PropModelo[HayarIndice(Nodos[j])] exists before getting .properties since if the previous one is undefined it does not have the property properties .

Test:

var tipo = typeof PropModelo[HayarIndice(Nodos[j])];
if (tipo === "undefined") ...

Here is a more complex example in which all the possibilities are tested:

var PropModelo = [
  {
    properties:[
      ['hola']
    ]
  },
  {
    sinproperties:[[]]
  },
];
console.log(PropModelo);

for (var i = 0; i < 2; i++) {
  if (typeof PropModelo[i] === 'undefined') {
    console.log("El índice PropModelo[" + i + "] no existe");
    continue;
  }
  if (typeof PropModelo[i].properties !== 'object') {
    console.log("PropModelo[" + i + "].properties no está definido");
    continue;
  }
  for (var a = 0; a < 2; a++) {
    if (typeof PropModelo[i].properties[a] === "undefined") {
      console.log("PropModelo[" + i + "].properties[" + a + "] no existe");
      continue;
    }
    for (var b = 0; b < 2; b++) {
      if (typeof PropModelo[i].properties[a][b] === "undefined") {
        console.log("PropModelo[" + i + "].properties[" + a + "][" + b + "] no existe");
      } else {
        console.log("Valor de PropModelo[" + i + "].properties[" + a + "][" + b + "]: " + PropModelo[i].properties[a][b]);
      }
    }
  }
}
/* Ahora en modo Chuck Norris, fíjate en el "undefined" de uno del (0,0,1) */
for (var i = 0; i < 2; i++) {
  for (var a = 0; a < 2; a++) {
    for (var b = 0; b < 2; b++) {
      try {
        console.log("Valor de PropModelo[" + i + "].properties[" + a + "][" + b + "]: " + PropModelo[i].properties[a][b]);
      } catch (e) {
        console.log("PropModelo[" + i + "].properties[" + a + "][" + b + "] no existe");
      }
    }
  }
}

Edit: I add block try/catch to show how it would work under it (notice the value undefined obtained when accessing a nonexistent index of an existing matrix.

    
answered by 20.01.2017 в 14:06
0

You almost have it. I believe that checking directly if undefined is worth (but without the quotes) if (array[propiedad]==undefined).....

    
answered by 20.01.2017 в 13:47