javascript - Node : TypeError: Cannot read property 'sort' of undefined when doing a findOneAndUpdate() inside of a find()

标签 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() 之前的点。我不明白为什么这会引起问题。这是怎么回事?

(注:这是类(class)作业 3-1 MongoDB for Node.js Developers 的一部分)

编辑:将空回调传递给 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)

最佳答案

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

所以你的查询将变成:

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

关于javascript - Node : TypeError: Cannot read property 'sort' of undefined when doing a findOneAndUpdate() inside of a find(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31106519/

相关文章:

javascript - GalleryView Jquery

node.js - Express 下更复杂的静态文件服务

MongoDB:在键中使用特殊字符查询哈希

java - 如何和平地裁剪现有的MongoDB?

javascript - 计算扑克牌局赔率

javascript - 如何使用 ng-repeat 在 Angular JS 中显示 json 响应值

javascript - 将 map 置于标记边界数组上 [传单]

node.js - Couchbase:无法对关闭的存储桶执行操作

node.js - 如何在 Insomnia GET 请求中设置 req.params?

node.js - 在 mongoose mongodb 中使用 $or 返回部分文档