使用findOneAndUpdate进行升级时出现错误
ValidatedCertificates.findOneAndUpdate(
//query
{ "course": req.body.course_name, "batch": req.body.batch_name },
{ //update
"issued": false,
"certificates": req.body.validatedBatch.certificates,
},
{ upsert: true },
{ useFindAndModify: false},
function (err, doc) {
if (err)
return res.json({
"status_code": 500,
"status_message": "Internal Error",
"data": err
});
else
return res.json({
"status_code": 200,
"status_message": "Validated Successfully",
});
});
}
但是我收到如下错误。
DeprecationWarning: Mongoose: `findOneAndUpdate()` and `findOneAndDelete()` without the `useFindAndModify` option set to false are deprecated. See: https://mongoosejs.com/docs/deprecations.html#-findandmodify-
updatd (节点:14179)UnhandledPromiseRejectionWarning:MongooseError:回调必须是一个函数,得到了[object Object]
我通过{upsert: true}
和{ useFindAndModify: false}
时有什么错误
答案 0 :(得分:2)
findOneAndUpdate方法签名与选项和回调类似:
findOneAndUpdate(conditions, update, options, callback)
第四参数必须是回调,但是您发送{ useFindAndModify: false}
。
因此,只需删除{ useFindAndModify: false}
即可。
要解决弃用错误,请将此{ useFindAndModify: false }
选项添加到mongoose.connect
mongoose.connect(uri, { useFindAndModify: false });
答案 1 :(得分:1)
要求:
{ upsert: true },
{ useFindAndModify: false}, //callback function
具有:
{ upsert: true}, //callbackfuntion
以及连接到server.js文件中的mongodb时 通过这样的配置:
mongoose
.connect(dbURL, {
useNewUrlParser: true,
useCreateIndex: true,
useUnifiedTopology: true,
useFindAndModify: false
})
.then(() => {
console.log(`mongoDb connection establish successfully at: ${dbURL}`);
});