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

319
Visualizações
how to handle Uncaught (in promise) TypeError: Cannot read properties of undefined (reading

I want to handle Uncaught (in promise):

TypeError: Cannot read properties of undefined (reading) this error in javascript. I want to alert if this type of error occurs....

How to do this in javascirpt ??

         if(typeof( dataName) === undefined)
                {
                    alert("undefined")
                }

enter image description here

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The error indicates that your getProduct function is async and that you try to access the property english_name for a variable that is undefined.

So if you want to handle that error you need to handle the error case for the promise chain at some point. Or ensure that the error does not happen at all:

async function getProduct() {
  let dataName = undefined;
  
  dataName.english_name = 1;
}


getProduct().catch(err => {
  console.error('error occured: ',err.message)
});

or

async function getProduct() {
  let dataName = undefined;

  dataName.english_name = 1;
}

async function run() {
  try {
    await getProduct();
  } catch (err) {
    console.error('error occured: ', err.message)
  }
}

run();

typeof returns a string and not undefined so it is either dataName === undefined or typeof( dataName) === 'undefined') to check if dataName is undefined:

async function getProduct() {
  let dataName = undefined;
  if (typeof(dataName) !== 'undefined') {
    dataName.english_name = 1;
  }

  if (dataName !== undefined) {
    dataName.english_name = 1;
  }
}

async function run() {
  try {
    await getProduct();
  } catch (err) {
    console.error('error occured: ', err.message)
  }
}

run();

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