Reputation: 410
I have got an error while doing upserting using 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",
});
});
}
But I'm getting an error like below.
DeprecationWarning: Mongoose: `findOneAndUpdate()` and `findOneAndDelete()` without the `useFindAndModify` option set to false are deprecated. See: https://mongoosejs.com/docs/deprecations.html#-findandmodify-
updatd (node:14179) UnhandledPromiseRejectionWarning: MongooseError: Callback must be a function, got [object Object]
Is there any mistake while I am passing {upsert: true}
and { useFindAndModify: false}
Upvotes: 1
Views: 2288
Reputation: 468
replace:
{ upsert: true },
{ useFindAndModify: false}, //callback function
with:
{ upsert: true}, //callbackfuntion
and at the time of connection to mongodb in server.js file pass configuration like this:
mongoose
.connect(dbURL, {
useNewUrlParser: true,
useCreateIndex: true,
useUnifiedTopology: true,
useFindAndModify: false
})
.then(() => {
console.log(`mongoDb connection establish successfully at: ${dbURL}`);
});
Upvotes: 3
Reputation: 17858
findOneAndUpdate method signature is like this with options and callback:
findOneAndUpdate(conditions, update, options, callback)
Fourth parameter must be callback, but you send { useFindAndModify: false}
.
So just remove { useFindAndModify: false}
and it will work.
And to resolve the deprecation error, add this { useFindAndModify: false }
option to the mongoose.connect
mongoose.connect(uri, { useFindAndModify: false });
Upvotes: 3