Node:TypeError:在find()

时间:2015-06-29 01:21:40

标签: javascript node.js mongodb

这是我的代码:

var MongoClient = require('mongodb').MongoClient;

MongoClient.connect('mongodb://localhost:27017/school', function(err, db) {
  if (err) throw err;

  db.collection('students').find().toArray(function(err, students) {
    students.forEach(function(student) {
      // get lower score
      var lowerScore = Infinity;
      for (var i = 0; i < student.scores.length; i++) {
        if (student.scores[i].type === 'homework' && student.scores[i].score < lowerScore) {
          lowerScore = student.scores[i].score;
        }
      }
      // remove the lower score
      var toRemove = {
        type: 'homework',
        score: lowerScore
      };
      db.collection('students').findOneAndUpdate(
        { _id: student._id }, 
        { $pull: { scores: toRemove } }
      );
    });
    db.close();
  });
});

这是我的输出:

~/code/m101js/hw3-1 $ node script.js
/Users/azerner/code/node_modules/mongodb/lib/utils.js:97
    process.nextTick(function() { throw err; });
                                        ^
TypeError: Cannot read property 'sort' of undefined
    at Collection.findOneAndUpdate (/Users/azerner/code/node_modules/mongodb/lib/collection.js:1378:14)
    at /Users/azerner/code/m101js/hw3-1/script.js:20:33
    at Array.forEach (native)
    at /Users/azerner/code/m101js/hw3-1/script.js:7:14
    at handleCallback (/Users/azerner/code/node_modules/mongodb/lib/utils.js:95:12)
    at /Users/azerner/code/node_modules/mongodb/lib/cursor.js:590:16
    at handleCallback (/Users/azerner/code/node_modules/mongodb/node_modules/mongodb-core/lib/cursor.js:234:5)
    at setCursorDeadAndNotified (/Users/azerner/code/node_modules/mongodb/node_modules/mongodb-core/lib/cursor.js:424:3)
    at nextFunction (/Users/azerner/code/node_modules/mongodb/node_modules/mongodb-core/lib/cursor.js:586:7)
    at Cursor.next (/Users/azerner/code/node_modules/mongodb/node_modules/mongodb-core/lib/cursor.js:614:3)
~/code/m101js/hw3-1 $

script.js:20:33指的是findOneAndUpdate()之前的那一点。我不明白为什么这会导致问题。这是怎么回事?

(注意:这是课程MongoDB for Node.js Developers

的作业3-1的一部分

编辑:将空回调传递给findOneAndUpdate()

时的输出
TypeError: Cannot read property 'sort' of null
    at Collection.findOneAndUpdate (/Users/azerner/code/node_modules/mongodb/lib/collection.js:1378:14)
    at /Users/azerner/code/m101js/hw3-1/script.js:20:33
    at Array.forEach (native)
    at /Users/azerner/code/m101js/hw3-1/script.js:7:14
    at handleCallback (/Users/azerner/code/node_modules/mongodb/lib/utils.js:95:12)
    at /Users/azerner/code/node_modules/mongodb/lib/cursor.js:590:16
    at handleCallback (/Users/azerner/code/node_modules/mongodb/node_modules/mongodb-core/lib/cursor.js:234:5)
    at setCursorDeadAndNotified (/Users/azerner/code/node_modules/mongodb/node_modules/mongodb-core/lib/cursor.js:424:3)
    at nextFunction (/Users/azerner/code/node_modules/mongodb/node_modules/mongodb-core/lib/cursor.js:586:7)
    at Cursor.next (/Users/azerner/code/node_modules/mongodb/node_modules/mongodb-core/lib/cursor.js:614:3)

1 个答案:

答案 0 :(得分:1)

你需要在$ pull之后添加{w:1}作为参数,这是驱动程序期望看到选项的地方,而且似乎需要findOneAndUpdate。

所以你的查询将成为:

 db.collection('students').findOneAndUpdate(
    { _id: student._id }, 
    { $pull: { scores: toRemove } },
    { w: 1 }
  );