如果我在我的 aws ubuntu 14.04 机器上不使用收缩包装或纱线,我的查询工作正常。
但是,相同的查询适用于本地计算机上的收缩包装/纱线,但不知何故它们在 aws 机器上不起作用。
查询如下所示:
var express = require('express');
var mongoose = require('mongoose');
var bodyParser = require('body-parser');
var foodtr = require('./../model/foodtruck');
var foodtrucklist = function(req, res) {
foodtr.find().populate('item_list').exec(function(err, foodtrucks) {
foodtrucks.forEach(function(ftr) {
var start_time = ftr.foodtruck_starting_timing;
var end_time = ftr.foodtruck_closing_timing;
var foodtruck_open_status = ftr.foodtruck_open_status;
// var shut_down = ftr.foodtruck_shutdown;
if ((start_time && end_time) &&
(start_time.trim() != '' &&
end_time.trim() != '')) {
if (inTime(start_time, end_time) &&
foodtruck_open_status ==0 ) {
ftr.foodtruck_open_status = 0;
ftr.save();
} else {
ftr.foodtruck_open_status = 1;
ftr.save();
}
}
})
res.json({
status: '200',
message: 'foodtrucklist',
data: foodtrucks
});
});
};
我得到的错误是:
events.js:160
throw er; // Unhandled 'error' event
^
TypeError: Cannot read property 'forEach' of undefined
at /home/bitnami/quflipNodeAPI/controller/foodtrucklist.js:9:19
at /home/bitnami/quflipNodeAPI/node_modules/mongoose/lib/query.js:2745:9
at newTickHandler (/home/bitnami/quflipNodeAPI/node_modules/mpromise/lib/promise.js:234:18)
at _combinedTickCallback (internal/process/next_tick.js:67:7)
at process._tickDomainCallback (internal/process/next_tick.js:122:9)
您需要处理错误:
foodtr.find().populate('item_list').exec(function(err, foodtrucks) {
// INSERT ERROR HANDLING HERE
foodtrucks.forEach(function(ftr) {
例如:
foodtr.find().populate('item_list').exec(function(err, foodtrucks) {
if (err) {
console.log('ERROR:', err);
return res.status(500).json({ error: err });
}
foodtrucks.forEach(function(ftr) {
仅当未定义err
时,才应访问foodtrucks
- 因为它只是已定义的一个 - err
或foodtrucks
。当您不检查错误时,您有时会访问未定义的数据,并且会收到这样的错误。