一个get请求中的多个猫鼬查询,节点js
Multiple mongoose queries in one get request, node js
嗨,我是节点的新手,我正在尝试 运行 一个请求中包含多个查询的猫鼬查询,我 运行 遇到了一个没有意义的问题。我正在检索存储在 mongodb 中的过去 7 天的能源历史数据。我从第 4 天到第 7 天注释掉请求有效,但是第 4 天到第 7 天的代码未注释,我抛出了这个错误:
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
});
});
我不明白为什么 fourDaysAgo 查询中的 history.kwhsDayAgo[0].fromgrid 没有从之前的代码中设置,如果不是的话,应该将猫鼬错误发送回请求。正如我已经说过的那样,代码在第 4 -7 天被注释掉时有效。
我觉得可能有更有效的方法来实现这一点,但我还没有找到任何人发布过类似的问题。如果有任何帮助,我将不胜感激。
您应该使用 Async 模块来填充历史对象。
安装模块
npm install async
首先require文件中的模块
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
。这样,您也可以访问之前函数返回的结果。但是,您必须在数组而不是对象中传递函数。
嗨,我是节点的新手,我正在尝试 运行 一个请求中包含多个查询的猫鼬查询,我 运行 遇到了一个没有意义的问题。我正在检索存储在 mongodb 中的过去 7 天的能源历史数据。我从第 4 天到第 7 天注释掉请求有效,但是第 4 天到第 7 天的代码未注释,我抛出了这个错误:
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
});
});
我不明白为什么 fourDaysAgo 查询中的 history.kwhsDayAgo[0].fromgrid 没有从之前的代码中设置,如果不是的话,应该将猫鼬错误发送回请求。正如我已经说过的那样,代码在第 4 -7 天被注释掉时有效。 我觉得可能有更有效的方法来实现这一点,但我还没有找到任何人发布过类似的问题。如果有任何帮助,我将不胜感激。
您应该使用 Async 模块来填充历史对象。
安装模块
npm install async
首先require文件中的模块
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
。这样,您也可以访问之前函数返回的结果。但是,您必须在数组而不是对象中传递函数。