Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

210
Views
¿Por qué no puedo detectar un valor desconocido en la matriz Array?

Necesito extraer datos de un sitio en formato json.

Estoy extrayendo con éxito estos datos y agregándolos a la matriz. Luego necesito usar los datos en esta cadena con un bucle for.

Pero a veces me sale el siguiente error. No he sido capaz de averiguar la razón de esto. Estoy enviando demasiadas solicitudes. (más de 500 en 5 segundos) El código da error al agregar el valor total como "indefinido" a la matriz Array o no. No pude resolverlo.

Al menos traté de mantener el código en ejecución si el valor total contiene "indefinido", pero aún fallé. Sigo recibiendo errores.

Estaría muy agradecido por cualquier ayuda. Perdón por mi mal ingles.

error:

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

código:

 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 answers
Answer question

0

El error se debe a que tradeCoinHistoryArray[i] no está definido.

Puede agregar un cheque:

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

0

Si su error ocurre en una línea, no puede esperar corregir un error después de ejecutar esa línea. Necesitas arreglar esa línea específica.

Por lo tanto, puede optar por definir un valor alternativo estático para esa variable, o debe definir la acción en ausencia de ese valor.

Esto evita el lanzamiento de errores, pero undefined se asignará a total .

 total = tradeCoinHistoryArray[i]?.total;

Esto asigna un valor alternativo a su total.

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

Si necesitas pegar es5

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

0

Puedes usar:

 if (tradeCoinHistoryArray[i]) { total = tradeCoinHistoryArray[i].total }
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!