Home > Mobile >  Postman convert a number into a string
Postman convert a number into a string

Time:07-11

I have an objet user with name, email and age and the las property is converted by postman into a string resulting in a 500 code error updating an user.

var mongoose = require('mongoose');

    var UserSchema = mongoose.Schema({
        name: {type: String, required: true},
        email: {type: String, required: true},
        age: {type: Number}
    }, {
        collection: 'users'
    }); //overrides default collection name auto created
    
    module.exports = mongoose.model('User', UserSchema);


    update: function(req, res)
    {
        //var params = JSON.parse(JSON.stringify(req.body));
        var params = req.body;
        console.log('params',params);
        var userId = req.params.id;
        console.log('userid', userId);
        User.findOneAndUpdate({ _id: userId }, params, { new: true }, (err, userUpdated) => {
    
            if (err) {
                return res.status(500).send({
                    status: 'error 500',
                    message: 'Error al actualizar usuario'
                });
            }
    
            if (!userUpdated) {
                return res.status(200).send({
                    status: 'error',
                    message: 'No se a actualizado el usuario'
                });
            }
    
            // Devolver respuesta
            return res.status(200).send({
                status: 'success',
                user: userUpdated
            });
    
        });
    },

I using nodejs and mocha con chai for the test. the test update is passing but with postman I got an error 500.

console.log(err); =>

params { '': '', name: 'pepita', email: '[email protected]', age: 22 }
userid 2c937b6fa7be991d306b7d6
CastError: Cast to ObjectId failed for value "2c937b6fa7be991d306b7d6" (type string) at path "_id" for model "User"
    at model.Query.exec (/var/www/html/tests-nodejs/node_modules/mongoose/lib/query.js:4719:21)
    at model.Query.Query.findOneAndUpdate (/var/www/html/tests-nodejs/node_modules/mongoose/lib/query.js:3390:8)
    at Function.Model.findOneAndUpdate (/var/www/html/tests-nodejs/node_modules/mongoose/lib/model.js:2595:13)
    at update (/var/www/html/tests-nodejs/controllers/user.js:89:10)
    at Layer.handle [as handle_request] (/var/www/html/tests-nodejs/node_modules/express/lib/router/layer.js:95:5)
    at next (/var/www/html/tests-nodejs/node_modules/express/lib/router/route.js:144:13)
    at Route.dispatch (/var/www/html/tests-nodejs/node_modules/express/lib/router/route.js:114:3)
    at Layer.handle [as handle_request] (/var/www/html/tests-nodejs/node_modules/express/lib/router/layer.js:95:5)
    at /var/www/html/tests-nodejs/node_modules/express/lib/router/index.js:284:15
    at param (/var/www/html/tests-nodejs/node_modules/express/lib/router/index.js:365:14) {
  messageFormat: undefined,
  stringValue: '"2c937b6fa7be991d306b7d6"',
  kind: 'ObjectId',
  value: '2c937b6fa7be991d306b7d6',
  path: '_id',
  reason: BSONTypeError: Argument passed in must be a string of 12 bytes or a string of 24 hex characters or an integer

CodePudding user response:

You could try ensuring the property which is supposed to be a number is a number before updating your DB.

req.body.age = Number(req.body.age)

Should work.

CodePudding user response:

I fixed the problem. I was passing bad the id, I missed one caracter. it is fixed.

  • Related