对于来自查询的数据,Foreach未定义

时间:2017-03-20 14:41:50

标签: node.js npm-shrinkwrap

如果我在我的aws ubuntu 14.04计算机上不使用 shrinkwrap yarn ,我的查询工作正常。

然而,同样的查询在本地机器上使用shrinkwrap / yarn工作正常,但不知何故他们不能在aws机器上工作。

查询如下所示:

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)

我得到的错误是:

-- see the log size
DBCC SQLPERF(LOGSPACE);

--taking backup for log file before shrink it
BACKUP LOG MyTestDB
TO DISK = 'E:\PartProcForOld_log_backup\MyTestDB.TRN'
GO

-- this command will tell you the log file name
SELECT name
FROM sys.master_files
WHERE database_id = db_id()
  AND type = 1


--- these below command will alter database with actual shrink
GO
-- Truncate the log by changing the database recovery model to SIMPLE.
ALTER DATABASE MyTestDB

SET RECOVERY SIMPLE;
GO
-- Shrink the truncated log file to 1 MB.
DBCC SHRINKFILE (MyTestDB_log, 1);
GO
-- Reset the database recovery model.
ALTER DATABASE MyTestDB
SET RECOVERY FULL;
GO

1 个答案:

答案 0 :(得分: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 - 因为它只是定义了errfoodtrucks。如果不检查错误,有时会访问未定义的数据,并且会出现这样的错误。