Tuesday, 25 April 2017

MySQL-NodeJS : "uncaughtException: Cannot read property '24' of undefined"

I make a function in node JS, in order to have all the datas from a MySQL database . When I type something like this, it works (all the callForecastADSL are initialized previously in the code)

  connection.query('SELECT * FROM db.vector WHERE type=\'ADSL\';', function (err, records){
        if(err) throw err;

        callForecastAdsl.type=records[0].type;
        });

But here, it doesn't work at all.

    for(i=0; i<96; i++)
      {
        for(j=0; j<24; j++)
        {
          for(k=0; k<7; k++)
          {
            connection.query('SELECT * FROM db.data WHERE vector_id=0 AND dept='+i+' AND hour='+j+' AND day='+k+';', function (err, records){
              if(err) throw err;
              console.log(callForecastAdsl.callForecastPerHourAndPerDay[i][j][k]);
              callForecastAdsl.callForecastPerHourAndPerDay[i][j][k].expected = records[0].expected;
              callForecastAdsl.callForecastPerHourAndPerDay[i][j][k].higherExpected = records[0].higherexpected;
              callForecastAdsl.callForecastPerHourAndPerDay[i][j][k].muchHigherExpected = records[0].muchhigherexpected;
            });
          }
       }
      }

I have a message error of the following type, that I dont understand :

error: uncaughtException: Cannot read property '24' of undefined date=Tue Apr 25 2017 09:10:33 GMT+0000 (UTC), pid=2632, uid=1002, gid=1002, cwd=/home/pfs/safeplus, execPath=/usr/bin/node, version=v7.6.0, argv=[/usr/bin/node, /home/team/project/server.js], rss=95518720, heapTotal=86962176, heapUsed=55731880, external=55911, loadavg=[0.4033203125, 0.09765625, 0.072265625], uptime=7507, ....

However, when I made outside the connection.query, it works.

console.log(callForecastADSL.callForecastPerHourAndPerDay[i][j][k]);

Also, in the connection.query, when I made console.log(records[0]); it works also.

So, how to explain this comportment ? And how to solve this error ?



via Simon Girard

No comments:

Post a Comment