mongoose: Cast to number failed for value "undefined"

Hi, with the latest version of mongoose, I have a mysterious problem. If I want to save document without specific typed field because it’s not required, mongoose return this error in my face :

Cast to number failed for value "undefined" at path "parent"

This is my model schema. It works fine in 3.8.24 :

var schema = mongoose.Schema({
    code                    : { type : String },
    plang                   : { type : String, required: true  },
    langs                   : [ { type : String, required : true } ],
    parent                 : { type: Number }
});

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Reactions: 3
  • Comments: 29 (2 by maintainers)

Commits related to this issue

Most upvoted comments

For me this is clearly a bug! Consider this code:

'use strict';

const mongoose = require('mongoose');
mongoose.Promise = Promise;
mongoose.connect('mongodb://localhost/test');

var FruitModel = mongoose.model('Test', mongoose.Schema({
    name: {
        type: String,
        unique: true
    },
    count: Number
}));

function updateOrInsertFailing(test) {
   return FruitModel.update({
    name: test.name
  }, test, {upsert: true});
}

function updateOrInsert(test) {
    return FruitModel.findOne({
        name: test.name
    }).then((stored) => {
        if (stored) {
            Object.assign(stored, test);
            return stored.save();
        } else {
            return FruitModel.create(test);
        }
    });
}

return Promise.all([
    updateOrInsert({
        name: 'apple',
        count: undefined
    }),
    updateOrInsertFailing({
        name: 'orange',
        count: undefined
    })
]).then(process.exit);