0

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

Here is schema:

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...

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?

Dmitry Klymenko
  • 173
  • 4
  • 11

1 Answers1

1

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}`);
})

Source: node-express error : express deprecated res.send(status): Use res.sendStatus(status) instead

thomann061
  • 629
  • 3
  • 11