1

如果我不在我的 aws ubuntu 14.04 机器上使用收缩包装纱线,我的查询工作正常。

但是,相同的查询可以在本地机器上使用 shrinkwrap/yarn,但不知何故,它们在 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)
4

1 回答 1

0

您需要处理错误:

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) {

您应该foodtrucks仅在err未定义时访问 - 因为它只是已定义的 -err或者foodtrucks. 当您不检查错误时,您有时会访问未定义的数据,您会收到这样的错误。

于 2017-03-20T14:52:45.133 回答