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

565
Visualizações
gettin this error in node jd error: TypeError: Cannot read property '0' of undefined

hi i am trying to implement a local orderbook using the binance api however i keep getting this error every now and then it does not always happen but if it does it will happen early on please help

this is the main file that runs calling an exports function from an external file to process the data coming from the web socket on message

wsClient.subscribeSpotDiffBookDepth("btcusdt");
wsClient.on('message', (data) => {

  // Setup and process order book information
  order_book.update_orderbook(data);
  if(order_book.ready === 1){
    order_book.get_orderbook_history();
  }

});

wsClient.on('error', err => {
  /* handle error */
    console.log("this is it 2 " + err);
});

wsClient.on('close', () => {
  /* ... */
});

exports function in external file

exports.update_orderbook = function(data) {
  if(data.e == "depthUpdate"){
    if(this.ready === 0){
      this.ready = 1;
      console.log("Stage 1 in play");
    }else{
      this.buffer += data;
    }

    if(this.ready === 2 && this.asks != null && this.bids != null){
      if(undefined !== this.asks && undefined !== this.bids && undefined !== data.a && undefined !== data.b){
        if(data.U <= this.lastUpdateId + 1 && data.u >= this.lastUpdateId + 1){

      // error is coming from calling this function-------------------------------------------------------
          var temp_array1 = sort_array(this.asks, data.a);
          var temp_array2 = sort_array(this.bids, data.b);
          this.asks = temp_array1;
          this.bids = temp_array2;
          this.lastUpdateId = data.u;

          console.log("Stage 3");
        }
      }
    }else{
      this.buffer += data;
    }
  }
}

function to sort and update the array

function sort_array(array1, array2){
  for(let x in array2){
    if(array2[x][1] == 0){
      for(let i in array2){
        if(array1[i][0] === array2[x][0]){
          array1.splice(i, 1);
        }
      }
    }else{
      var in_array = false;
      for(let i in array1){
        // this seems to be the problem area---------------------------------------------------------
        if(array1[i][0] === array2[x][0]){ 
          array1[i] = array2[x];
          in_array = true;
        }
      }
      if(!in_array){
        array1.push(array2[x]);
      }
    }
  }
  return array1;
}

error log

enter image description here

about 4 years ago · Juan Pablo Isaza
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