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

200
Visualizações
Why can't I detect unknown value in Array array?

I need to pull data from a site in json format.

I am successfully pulling this data and adding it to the array. I then need to use the data in this string with a for loop.

But sometimes I get the following error. I haven't been able to figure out the reason for this. I'm sending too many requests. (more than 500 in 5 seconds) The code gives an error when adding the total value as "undefined" to the Array array or not. I couldn't solve it.

At least I tried to keep the code running if the total value contains "undefined" but still failed. I keep getting errors.

I would be very grateful for any help. Sorry for my bad english.

error:

var totalArr = tradeCoinHistoryArray[i].total;         
TypeError: Cannot read property 'total' of undefined

code:

request(TRADE_HISTORY_URL + coin, function (error, response, body) {
      if (!error && response.statusCode == 200) {
          var importedJSON = JSON.parse(body);
  
          tradeCoinHistoryArray = importedJSON.data;   

              for (var i = tradeCoinHistoryArray.length - TRADE_LAST_TRADE_COUNT; i < tradeCoinHistoryArray.length; i++) {

                //error line
                total = tradeCoinHistoryArray[i].total;
                //error line


                if(total == undefined){
                  //console.log("continue if total is undefined";
                  return
                }
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

The error is due to tradeCoinHistoryArray[i] object not being defined.

You can add a check:

if (tradeCoinHistoryArray[i] && tradeCoinHistoryArray[i].hasOwnProperty('total')) {
     total = tradeCoinHistoryArray[i].total;
}
about 4 years ago · Juan Pablo Isaza Relatório

0

If your error happening on a line you can't expect to correct an error after executing that line. You need to fix that specific line.

So you may choose to define a static alternate value for that variable, or you should define action in the absence of that value.

This prevents error throw, but undefined will be assigned to total.

total = tradeCoinHistoryArray[i]?.total;

This assigns an alternate value to your total.

total = (tradeCoinHistoryArray[i]?.total) ? tradeCoinHistoryArray[i].total : 0;

If you need to stick es5

total = (tradeCoinHistoryArray[i].total !== undefined) ? tradeCoinHistoryArray[i].total:0
about 4 years ago · Juan Pablo Isaza Relatório

0

You can use:

if (tradeCoinHistoryArray[i]) {
    total = tradeCoinHistoryArray[i].total
}
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