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

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

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 Report

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 Report

0

You can use:

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!