Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

233
Visualizações
Uncaught (in promise) TypeError: Cannot read properties of null (reading 'iso3166_1Alpha2') / Trying to skip null but cannot

I have a global list of mountains. I want to filter all French mountains. To do this, I need to check if iso3166_1Alpha2 is set to FR. The problem is that not all mountains have a value. The script dies after it hits a null value I think because this is the error:

Uncaught (in promise) TypeError: Cannot read properties of null (reading 'iso3166_1Alpha2')

This is my script. Seems my check of !== null is not working and do not know why.

function addJSON() {
  let url = "https://development.example.com/admin/mtn/json/mtn_areas.geojson";
  fetch(url)
    .then(function (response) {
      return response.json();
    })
    .then(function (data) {
      let mtn = data.features;
      for (var i = 0; i < mtn.length; i++) {
        if (mtn[i].properties.location.iso3166_1Alpha2 !== null) {
          x = mtn[i].properties.location.iso3166_1Alpha2;
          console.log(x);
        }
      }
    });
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

It seems like your are getting an item which doesn't have a location property, in which case your condition won't help preventing it. Try changin your function as below. Notice I'm using Optional chaining, the ?. syntax, to avoid any null or undifined property.

function addJSON() {
  let url = "https://development.example.com/admin/mtn/json/mtn_areas.geojson";
  fetch(url)
    .then(function (response) {
      return response.json();
    })
    .then(function (data) {
      let mtn = data.features;
      for (var i = 0; i < mtn.length; i++) {
        if (mtn[i]?.properties?.location?.iso3166_1Alpha2) {
          x = mtn[i].properties.location.iso3166_1Alpha2;
          console.log(x);
        }
      }
    });
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda