Mongoose findByIdAndUpdate 成功更新文档但出现错误 [英] Mongoose findByIdAndUpdate successfully updates document but ruturns error

查看:39
本文介绍了Mongoose findByIdAndUpdate 成功更新文档但出现错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

findByIdAndUpdate() 成功更新文档,但返回我不明白的错误.

findByIdAndUpdate() successfully updates document, but returns error which i don't understand.

这是架构:

const userSchema = mongoose.Schema({
    phone: String,
    password: String,
    token: String
});
const User = mongoose.model('User', userSchema);

这里是更新数据库中用户的函数

And here is function to update user in database

export const login = (req, res) => {
    User.findOne({ phone: req.body.phone }, (err, result) => {
        if (err) res.status(500).send(`User with ${req.body.phone} doesn't exist. \n Error: ${err}`);
        if( result.password === req.body.password ){
            // here Console.log(result) returns:
            //{ 
            //  _id: 5aa28eb4f4a8de28c24e6990,
            //  phone: '+79781231233434',
            //  password: 'passsss',
            //  token: '1520613346284',
            //  __v: 0 
            //}

            User.findByIdAndUpdate( result.id, { "token": Date.now() },
                (err, result) => {
                    // It gives error, of which stacktrace i give below. But if check database - 
                    // everything is fine, token was updated successfully
                    if (err) return res.status(500).send('Unable to create token. Error: ', err);
                    return res.status(200).send(result._id, result.token);
            })
        } else return res.status(500).send('Incorrect password');
    })
}

这是我在发布请求时在控制台中得到的内容,这些数据应该可以成功通过此检查并获取令牌.

Here is what i get in console when i do post request with data which should successfully pass this check and get token.

express deprecated res.send(status, body): Use res.status(status).send(body) instead controllers/user.js:17:28
express deprecated res.send(status, body): Use res.status(status).send(body) instead controllers/user.js:16:37
/Users/dmitryklymenko/Documents/projects/project_exchange/server/node_modules/mongoose/lib/utils.js:423
        throw err;
        ^

RangeError: Invalid status code: Unable to create token. Error: 
    at ServerResponse.writeHead (_http_server.js:190:11)
    at ServerResponse._implicitHeader (_http_server.js:181:8)
    at write_ (_http_outgoing.js:635:9)
    at ServerResponse.end (_http_outgoing.js:754:5)
    at ServerResponse.send (/Users/dmitryklymenko/Documents/projects/project_exchange/server/node_modules/express/lib/response.js:221:10)
    at ServerResponse.json (/Users/dmitryklymenko/Documents/projects/project_exchange/server/node_modules/express/lib/response.js:267:15)
    at ServerResponse.send (/Users/dmitryklymenko/Documents/projects/project_exchange/server/node_modules/express/lib/response.js:158:21)
    at /Users/dmitryklymenko/Documents/projects/project_exchange/server/controllers/user.js:10:38
    at /Users/dmitryklymenko/Documents/projects/project_exchange/server/node_modules/mongoose/lib/model.js:3930:16
    at _init (/Users/dmitryklymenko/Documents/projects/project_exchange/server/node_modules/mongoose/lib/query.js:2000:14)
    at completeOne (/Users/dmitryklymenko/Documents/projects/project_exchange/server/node_modules/mongoose/lib/query.js:1995:5)
    at cb (/Users/dmitryklymenko/Documents/projects/project_exchange/server/node_modules/mongoose/lib/query.js:2365:14)
    at /Users/dmitryklymenko/Documents/projects/project_exchange/server/node_modules/mongoose/lib/query.js:2465:14
    at /Users/dmitryklymenko/Documents/projects/project_exchange/server/node_modules/mongoose/lib/utils.js:418:16
    at result (/Users/dmitryklymenko/Documents/projects/project_exchange/server/node_modules/mongodb/lib/utils.js:413:17)
    at session.endSession (/Users/dmitryklymenko/Documents/projects/project_exchange/server/node_modules/mongodb/lib/utils.js:400:11)
    at ClientSession.endSession (/Users/dmitryklymenko/Documents/projects/project_exchange/server/node_modules/mongodb-core/lib/sessions.js:69:41)
    at executeCallback (/Users/dmitryklymenko/Documents/projects/project_exchange/server/node_modules/mongodb/lib/utils.js:396:17)
    at handleCallback (/Users/dmitryklymenko/Documents/projects/project_exchange/server/node_modules/mongodb/lib/utils.js:128:55)
    at /Users/dmitryklymenko/Documents/projects/project_exchange/server/node_modules/mongodb/lib/collection.js:2302:12
    at result (/Users/dmitryklymenko/Documents/projects/project_exchange/server/node_modules/mongodb/lib/utils.js:413:17)
    at executeCallback (/Users/dmitryklymenko/Documents/projects/project_exchange/server/node_modules/mongodb/lib/utils.js:405:9)
[nodemon] app crashed - waiting for file changes before starting...

我不明白这个错误.为什么会出现?如果更新完成,为什么会出现错误?为什么我在任何地方都使用 res.status(status).send(body) 时会看到有关弃用的警告?也许是 Babel 给我带来了问题?有人知道我需要检查什么才能弄清楚发生了什么吗?

I don't understand this error. Why it appears? If update done, why there is an error at all? And why i see warning about deprecations if i use res.status(status).send(body), as it suggests, everywhere? Maybe it's Babel makes problems to me? Anybody know what i need to check to figure out whats going on?

推荐答案

body 参数可以是 Buffer 对象、String、对象或数组.

The body parameter can be a Buffer object, a String, an object, or an Array.

为了使其正常工作,请使用字符串插值:

For it to work properly, use string interpolation:

User.findByIdAndUpdate( result.id, { "token": Date.now() },
  (err, result) => {
    // It gives error, of which stacktrace i give below. But if check database - 
    // everything is fine, token was updated successfully
    if (err) return res.status(500).send(`Unable to create token. Error: ${err}`);
      return res.status(200).send(`${result._id}, ${result.token}`);
})

来源:node-express 错误:不推荐使用快递res.send(status): 使用 res.sendStatus(status) 代替

这篇关于Mongoose findByIdAndUpdate 成功更新文档但出现错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆