简体   繁体   English

Mongoose- 使用 findOneAndUpdate 更新插入时出错

[英]Mongoose- Error while upserting using findOneAndUpdate

I have got an error while doing upserting using findOneAndUpdate使用 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] 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}我传递{upsert: true}{ useFindAndModify: false}时是否有任何错误

reqplace:请求位置:

{ 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:并在 server.js 文件中连接到 mongodb 时,通过配置如下:

 mongoose
      .connect(dbURL, {
        useNewUrlParser: true,
        useCreateIndex: true,
        useUnifiedTopology: true,
        useFindAndModify: false
      })
      .then(() => {
        console.log(`mongoDb connection establish successfully at: ${dbURL}`);
      });

findOneAndUpdate method signature is like this with options and callback: findOneAndUpdate方法签名是这样的,带有选项和回调:

findOneAndUpdate(conditions, update, options, callback)

Fourth parameter must be callback, but you send { useFindAndModify: false} .第四个参数必须是回调,但您发送{ useFindAndModify: false} So just remove { useFindAndModify: false} and it will work.所以只需删除{ useFindAndModify: false}就可以了。

And to resolve the deprecation error, add this { useFindAndModify: false } option to the mongoose.connect要解决弃用错误,请将此{ useFindAndModify: false }选项添加到 mongoose.connect

mongoose.connect(uri, { useFindAndModify: false });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM