2015-04-07 71 views
0

您好我是新來的節點,我試圖在一個請求中運行帶有多個查詢的貓鼬查詢,並且遇到了一個沒有意義的問題。我正在檢索存儲在過去7天內的mongodb中的能量歷史數據。我從7請求工作4天的徵求意見,但是從當天碼4 - 7註釋掉我拋出這個錯誤:在一個獲取請求中的多個貓鼬查詢,節點js

story.kwhsNow[0].fromgrid - data[0].fromgrid - history.kwhsDayAgo[0].fromgrid 
                   ^TypeError: Cannot read property 'fromgrid' of undefined 

這個fourDaysAgo查詢後發生。

這裏是在請求代碼:

httpsRouter.get('/api/weekhistory', function(req, res) { 

    var history = {kwhsNow: '', kwhsToday: '', kwhsDayAgo: '', kwhsTwoDaysAgo: '', kwhsThreeDaysAgo: '', kwhsFourDaysAgo: '', kwhsFiveDaysAgo: '', kwhsSixDaysAgo: '', kwhsSevenDaysAgo: ''}; 

     kwhsNowQuery = eagleData.eagleKwhs.find(), 
     kwhsNowQuery.sort('-_id'); 
     kwhsNowQuery.limit(1); 
     kwhsNowQuery.exec(function(err, data) { 
     // if there is an error retrieving, send the error. nothing after res.send(err) will execute 
     if (err) 
      res.send(err) 

     history.kwhsNow = data; 
    });  

    var sinceToday = moment().hours(0).minutes(0).seconds(0).format('x'); 

    var sinceTodayQuery = eagleData.eagleKwhs.find(); 
     sinceTodayQuery.where('_id').gte(sinceToday - 10000).lte(sinceToday + 10000); 
     sinceTodayQuery.limit(1); 
     sinceTodayQuery.exec(function(err, data) { 
     // if there is an error retrieving, send the error. nothing after res.send(err) will execute 
     if (err) 
      res.send(err) 
     console.log('Since today: ',data[0].fromgrid); 
     data[0].fromgrid = history.kwhsNow[0].fromgrid - data[0].fromgrid; 
     history.kwhsToday = data; 

    }); 

    var dayAgo = moment().subtract(1, 'days').hours(0).minutes(0).seconds(0).format('x'); 

    var dayAgoQuery = eagleData.eagleKwhs.find(); 
     dayAgoQuery.where('_id').gte(dayAgo - 10000).lte(dayAgo + 10000); 
     dayAgoQuery.limit(1); 
     dayAgoQuery.exec(function(err, data) { 
     // if there is an error retrieving, send the error. nothing after res.send(err) will execute 
     if (err) 
      res.send(err) 
     console.log('Day Ago: ',data[0].fromgrid);  
     data[0].fromgrid = history.kwhsNow[0].fromgrid - data[0].fromgrid - history.kwhsToday[0].fromgrid; 
     history.kwhsDayAgo = data; 
     res.json(history); 
     console.log(history);// return all in JSON format  
    });   

    var twoDaysAgo = moment().subtract(2, 'days').hours(0).minutes(0).seconds(0).format('x'); 

    var twoDaysAgoQuery = eagleData.eagleKwhs.find(); 
     twoDaysAgoQuery.where('_id').gte(twoDaysAgo - 10000).lte(twoDaysAgo + 10000); 
     twoDaysAgoQuery.limit(1); 
     twoDaysAgoQuery.exec(function(err, data) { 
     // if there is an error retrieving, send the error. nothing after res.send(err) will execute 
     if (err) 
      res.send(err) 

     console.log('Two Days Ago: ',data[0].fromgrid);  
     data[0].fromgrid = history.kwhsNow[0].fromgrid - data[0].fromgrid - history.kwhsToday[0].fromgrid - history.kwhsDayAgo[0].fromgrid; 
     history.kwhsTwoDaysAgo = data; 

    }); 

    var threeDaysAgo = moment().subtract(3, 'days').hours(0).minutes(0).seconds(0).format('x'); 

    var threeDaysAgoQuery = eagleData.eagleKwhs.find(); 
     threeDaysAgoQuery.where('_id').gte(threeDaysAgo - 10000).lte(threeDaysAgo + 10000); 
     threeDaysAgoQuery.limit(1); 
     threeDaysAgoQuery.exec(function(err, data) { 
     // if there is an error retrieving, send the error. nothing after res.send(err) will execute 
     if (err) 
      res.send(err) 

     console.log('Three Days Ago: ',data[0].fromgrid);  
     data[0].fromgrid = history.kwhsNow[0].fromgrid - data[0].fromgrid - history.kwhsToday[0].fromgrid - history.kwhsDayAgo[0].fromgrid - history.twoDaysAgo[0].fromgrid; 
     history.kwhsThreeDaysAgo = data; 
    }); 


    var fourDaysAgo = moment().subtract(4, 'days').hours(0).minutes(0).seconds(0).format('x');  

    var fourDaysAgoQuery = eagleData.eagleKwhs.find(); 
     fourDaysAgoQuery.where('_id').gte(fourDaysAgo - 10000).lte(fourDaysAgo + 10000); 
     fourDaysAgoQuery.limit(1); 
     fourDaysAgoQuery.exec(function(err, data) { 
     //if there is an error retrieving, send the error. nothing after res.send(err) will execute 
     if (err) 
      res.send(err) 

     console.log('Four Days Ago: ',data[0].fromgrid);     //history.kwhsToday[0].fromgrid - 
     data[0].fromgrid = history.kwhsNow[0].fromgrid - data[0].fromgrid - history.kwhsDayAgo[0].fromgrid - history.twoDaysAgo[0].fromgrid - history.threeDaysAgo[0].fromgrid; 
     history.kwhsFourDaysAgo = data; 
    }); 

    var fiveDaysAgo = moment().subtract(5, 'days').hours(0).minutes(0).seconds(0).format('x');  

    var fiveDaysAgoQuery = eagleData.eagleKwhs.find(); 
     fiveDaysAgoQuery.where('_id').gte(fiveDaysAgo - 10000).lte(fiveDaysAgo + 10000); 
     fiveDaysAgoQuery.limit(1); 
     fiveDaysAgoQuery.exec(function(err, data) { 
     // if there is an error retrieving, send the error. nothing after res.send(err) will execute 
     if (err) 
      res.send(err) 

     console.log('Five Days Ago: ',data[0].fromgrid);  
     data[0].fromgrid = history.kwhsNow[0].fromgrid - data[0].fromgrid - history.kwhsToday[0].fromgrid - history.kwhsDayAgo[0].fromgrid - history.twoDaysAgo[0].fromgrid - history.threeDaysAgo[0].fromgrid - history.fourDaysAgo[0].fromgrid; 
     history.kwhsFiveDaysAgo = data; 
    }); 

var sixDaysAgo = moment().subtract(6, 'days').hours(0).minutes(0).seconds(0).format('x');  

    var sixDaysAgoQuery = eagleData.eagleKwhs.find(); 
     sixDaysAgoQuery.where('_id').gte(sixDaysAgo - 10000).lte(sixDaysAgo + 10000); 
     sixDaysAgoQuery.limit(1); 
     sixDaysAgoQuery.exec(function(err, data) { 
     // if there is an error retrieving, send the error. nothing after res.send(err) will execute 
     if (err) 
      res.send(err) 

     console.log('Six Days Ago: ',data[0].fromgrid);  
     data[0].fromgrid = history.kwhsNow[0].fromgrid - data[0].fromgrid - history.kwhsToday[0].fromgrid - history.kwhsDayAgo[0].fromgrid - history.twoDaysAgo[0].fromgrid - history.threeDaysAgo[0].fromgrid - history.fourDaysAgo[0].fromgrid - history.fiveDaysAgo[0].fromgrid; 
     history.kwhsSixDaysAgo = data; 
    }); 

var sevenDaysAgo = moment().subtract(7, 'days').hours(0).minutes(0).seconds(0).format('x');  

    var sevenDaysAgoQuery = eagleData.eagleKwhs.find(); 
     sevenDaysAgoQuery.where('_id').gte(sevenDaysAgo - 10000).lte(sevenDaysAgo + 10000); 
     sevenDaysAgoQuery.limit(1); 
     sevenDaysAgoQuery.exec(function(err, data) { 
     // if there is an error retrieving, send the error. nothing after res.send(err) will execute 
     if (err) 
      res.send(err) 

     console.log('Seven Days Ago: ',data[0].fromgrid);  
     data[0].fromgrid = history.kwhsNow[0].fromgrid - data[0].fromgrid - history.kwhsToday[0].fromgrid - history.kwhsDayAgo[0].fromgrid - history.twoDaysAgo[0].fromgrid - history.threeDaysAgo[0].fromgrid - history.fourDaysAgo[0].fromgrid - history.fiveDaysAgo[0].fromgrid - history.sixDaysAgo[0].fromgrid; 
     history.kwhsSevenDaysAgo = data; 
     res.json(history); 
     console.log(history);// return all in JSON format 
    }); 

}); 

我不明白爲什麼history.kwhsDayAgo .fromgrid在fourDaysAgo查詢尚未從以前的代碼集和[0]如果不是貓鼬錯誤應該已被送回req。正如我已經說過的,代碼在第4 - 7天被註釋掉時起作用。 我覺得可以有更高效的方式來實現這一目標,但我還沒有找到任何發佈過類似問題的人。我會很感激任何幫助。

+0

使用異步模塊 – ZeMoon

回答

1

您應該使用Async模塊填充歷史記錄對象。

安裝模塊

npm install async 

首先需要在請求處理程序的文件中的模塊

var Async = require('async'); 

然後:

var history = { 

    kwhsNow: function (callback) { 
     kwhsNowQuery = eagleData.eagleKwhs.find(), 
      kwhsNowQuery.sort('-_id'); 
      kwhsNowQuery.limit(1); 
      kwhsNowQuery.exec(function(err, data) { 
      // if there is an error retrieving, send the error. nothing after res.send(err) will execute 
      if (err) 
       return callback(err) 

      callback(null, data); 
     }); 
    }, 

    kwhsToday: function (callback) { 
     var sinceToday = moment().hours(0).minutes(0).seconds(0).format('x'); 

     var sinceTodayQuery = eagleData.eagleKwhs.find(); 
      sinceTodayQuery.where('_id').gte(sinceToday - 10000).lte(sinceToday + 10000); 
      sinceTodayQuery.limit(1); 
      sinceTodayQuery.exec(function(err, data) { 
      // if there is an error retrieving, send the error. nothing after res.send(err) will execute 
      if (err) 
       return callback(err); 

      console.log('Since today: ',data[0].fromgrid); 
      data[0].fromgrid = history.kwhsNow[0].fromgrid - data[0].fromgrid; 
      callback (null, data); 

     }); 
    }, 

    kwhsDayAgo: function (callback) { 
     var dayAgo = moment().subtract(1, 'days').hours(0).minutes(0).seconds(0).format('x'); 

     var dayAgoQuery = eagleData.eagleKwhs.find(); 
      dayAgoQuery.where('_id').gte(dayAgo - 10000).lte(dayAgo + 10000); 
      dayAgoQuery.limit(1); 
      dayAgoQuery.exec(function(err, data) { 
      // if there is an error retrieving, send the error. nothing after res.send(err) will execute 
      if (err) 
       return callback(err); 

      console.log('Day Ago: ',data[0].fromgrid);  
      data[0].fromgrid = history.kwhsNow[0].fromgrid - data[0].fromgrid - history.kwhsToday[0].fromgrid; 
      callback (null, data); 
      res.json(history); 
      console.log(history);// return all in JSON format  
     }); 
    }, 

    kwhsTwoDaysAgo: function (callback) { 
     var twoDaysAgo = moment().subtract(2, 'days').hours(0).minutes(0).seconds(0).format('x'); 

     var twoDaysAgoQuery = eagleData.eagleKwhs.find(); 
     twoDaysAgoQuery.where('_id').gte(twoDaysAgo - 10000).lte(twoDaysAgo + 10000); 
     twoDaysAgoQuery.limit(1); 
     twoDaysAgoQuery.exec(function(err, data) { 
     // if there is an error retrieving, send the error. nothing after res.send(err) will execute 
      if (err) 
       return callback(err); 

      console.log('Two Days Ago: ',data[0].fromgrid);  
      data[0].fromgrid = history.kwhsNow[0].fromgrid - data[0].fromgrid - history.kwhsToday[0].fromgrid - history.kwhsDayAgo[0].fromgrid; 
      callback (null, data); 

     }); 

    }, kwhsThreeDaysAgo: function (callback) { 
      var threeDaysAgo = moment().subtract(3, 'days').hours(0).minutes(0).seconds(0).format('x'); 

      var threeDaysAgoQuery = eagleData.eagleKwhs.find(); 
      threeDaysAgoQuery.where('_id').gte(threeDaysAgo - 10000).lte(threeDaysAgo + 10000); 
      threeDaysAgoQuery.limit(1); 
      threeDaysAgoQuery.exec(function(err, data) { 
      // if there is an error retrieving, send the error. nothing after res.send(err) will execute 
      if (err) 
       return callback(err); 

      console.log('Three Days Ago: ',data[0].fromgrid);  
      data[0].fromgrid = history.kwhsNow[0].fromgrid - data[0].fromgrid - history.kwhsToday[0].fromgrid - history.kwhsDayAgo[0].fromgrid - history.twoDaysAgo[0].fromgrid; 
      callback (null, data); 
     }); 

    }, kwhsFourDaysAgo: function (callback) { 
      var fourDaysAgo = moment().subtract(4, 'days').hours(0).minutes(0).seconds(0).format('x');  

      var fourDaysAgoQuery = eagleData.eagleKwhs.find(); 
      fourDaysAgoQuery.where('_id').gte(fourDaysAgo - 10000).lte(fourDaysAgo + 10000); 
      fourDaysAgoQuery.limit(1); 
      fourDaysAgoQuery.exec(function(err, data) { 
      //if there is an error retrieving, send the error. nothing after res.send(err) will execute 
      if (err) 
       return callback(err); 

      console.log('Four Days Ago: ',data[0].fromgrid);     //history.kwhsToday[0].fromgrid - 
      data[0].fromgrid = history.kwhsNow[0].fromgrid - data[0].fromgrid - history.kwhsDayAgo[0].fromgrid - history.twoDaysAgo[0].fromgrid - history.threeDaysAgo[0].fromgrid; 
      callback (null, data); 
     }); 
    }, kwhsFiveDaysAgo: function (callback) { 
     var fiveDaysAgo = moment().subtract(5, 'days').hours(0).minutes(0).seconds(0).format('x');  

     var fiveDaysAgoQuery = eagleData.eagleKwhs.find(); 
      fiveDaysAgoQuery.where('_id').gte(fiveDaysAgo - 10000).lte(fiveDaysAgo + 10000); 
      fiveDaysAgoQuery.limit(1); 
      fiveDaysAgoQuery.exec(function(err, data) { 
      // if there is an error retrieving, send the error. nothing after res.send(err) will execute 
      if (err) 
       return callback(err); 

      console.log('Five Days Ago: ',data[0].fromgrid);  
      data[0].fromgrid = history.kwhsNow[0].fromgrid - data[0].fromgrid - history.kwhsToday[0].fromgrid - history.kwhsDayAgo[0].fromgrid - history.twoDaysAgo[0].fromgrid - history.threeDaysAgo[0].fromgrid - history.fourDaysAgo[0].fromgrid; 
      callback (null, data); 
     }); 
    }, 

    kwhsSixDaysAgo: function (callback) { 

     var sixDaysAgo = moment().subtract(6, 'days').hours(0).minutes(0).seconds(0).format('x');  

     var sixDaysAgoQuery = eagleData.eagleKwhs.find(); 
      sixDaysAgoQuery.where('_id').gte(sixDaysAgo - 10000).lte(sixDaysAgo + 10000); 
      sixDaysAgoQuery.limit(1); 
      sixDaysAgoQuery.exec(function(err, data) { 
      // if there is an error retrieving, send the error. nothing after res.send(err) will execute 
      if (err) 
       return callback(err); 

      console.log('Six Days Ago: ',data[0].fromgrid);  
      data[0].fromgrid = history.kwhsNow[0].fromgrid - data[0].fromgrid - history.kwhsToday[0].fromgrid - history.kwhsDayAgo[0].fromgrid - history.twoDaysAgo[0].fromgrid - history.threeDaysAgo[0].fromgrid - history.fourDaysAgo[0].fromgrid - history.fiveDaysAgo[0].fromgrid; 
      callback (null, data); 
     }); 
    }, 

    kwhsSevenDaysAgo: function (callback){ 
     var sevenDaysAgo = moment().subtract(7, 'days').hours(0).minutes(0).seconds(0).format('x');  

     var sevenDaysAgoQuery = eagleData.eagleKwhs.find(); 
      sevenDaysAgoQuery.where('_id').gte(sevenDaysAgo - 10000).lte(sevenDaysAgo + 10000); 
      sevenDaysAgoQuery.limit(1); 
      sevenDaysAgoQuery.exec(function(err, data) { 
      // if there is an error retrieving, send the error. nothing after res.send(err) will execute 
      if (err) 
       return callback(err); 

      console.log('Seven Days Ago: ',data[0].fromgrid);  
      data[0].fromgrid = history.kwhsNow[0].fromgrid - data[0].fromgrid - history.kwhsToday[0].fromgrid - history.kwhsDayAgo[0].fromgrid - history.twoDaysAgo[0].fromgrid - history.threeDaysAgo[0].fromgrid - history.fourDaysAgo[0].fromgrid - history.fiveDaysAgo[0].fromgrid - history.sixDaysAgo[0].fromgrid; 
      callback (null, data); 
      res.json(history); 
      console.log(history);// return all in JSON format 
     }); 
    } 
}; 

Async.parallel (history, function (err, results) { 

    if (err) 
     throw err; 

    //results holds the data in object form 
    console.log(results); 

}); 

編輯: Async.Parallel執行函數同時傳遞給它。如果您想讓它們依次執行,請使用Async.Series。這樣,您將可以訪問前面功能返回的結果。但是,您將不得不在數組中傳遞函數而不是對象。

+0

感謝您的快速響應。我一直在閱讀異步模塊。我已經更新了代碼,但是每個函數仍然被異步觸發,這意味着當我編寫代碼時,在處理數據時遇到同樣的問題。當我註釋掉計算(減法)時,對象被填充,但不按順序填充。 – Matt

+0

{kwhsNow:[{_id:1428426599000,fromgrid:54137719}], kwhsFourDaysAgo:[{_id:1428044400000,fromgrid:54022614}], kwhsToday:[{_id:14283.9億,fromgrid:54123738}], kwhsFiveDaysAgo: {_id:14279.58億,fromgrid:53997075}], kwhsDayAgo:[{_id:1428303600000,fromgrid:54092976}], kwhsSixDaysAgo:[{_id:1427871602000,fromgrid:53975853}], kwhsTwoDaysAgo:[{_id:1428217200000, fromgrid:54063325}], kwhsSevenDaysAgo:[{_id:1427785200000,fromgrid:53954110}], kwhsThreeDaysAgo:[{_id:1428156300000,fromgrid:54048604}]} – Matt

+0

我假設這是節點工作原理和每個db查詢在不同的時間返回,這解釋了爲什麼kwhsToday在kwhsFourDaysAgo函數中未定義。我將從每個函數中刪除我的計算並在最後處理數據。 – Matt