gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 09:43:36 25 4
gpt4 key购买 nike

这是我的代码:

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com