这是我的代码:
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)
最佳答案
您需要在$ pull之后添加{w:1}作为参数,这是驱动程序希望看到选项的位置,并且似乎需要findOneAndUpdate。
因此,您的查询将变为:
db.collection('students').findOneAndUpdate(
{ _id: student._id },
{ $pull: { scores: toRemove } },
{ w: 1 }
);
关于javascript - Node :TypeError:在find()内部执行findOneAndUpdate()时,无法读取未定义的属性“sort”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31106519/