3

Running into a CastError in Mongoose when populating nested ObjectId references (value {}) that are apparently valid, to the extent that they're not blocked when saving to schema.

Interested in resolving this on the server-side to prevent malformed data in future, however, I understand not saving these values from the client would be a good idea. The fact that an empty object can be saved to a Mongoose schema with type: mongoose.Schema.Types.ObjectId and later throws a CastError using population is my primary concern.

Example data:

// representation of data in model
{
    "_id": /* ObjectId */,
    "refA": {} // empty object,
    "refB": /* ObjectId */,
    "refC": /* ObjectId */
}

Example method:

// mongoose query with population
function populator(id, cb) {
    // find by string or object id
    var query = Schema.findById(id);

    // population of query
    query.populate({
        // string of fields to expand
        path: 'refA refB refC',
        // option to include virtuals
        options: {toJSON: {virtuals: true}}
    });

    // return executed query,
    // optional callback
    return _.isFunction(cb) ? query.exec(cb) : query.exec();
}

Resulting error:

// error in console
{ [CastError: Cast to ObjectId failed for value "[object Object]" at path "_id"]
    message: 'Cast to ObjectId failed for value "[object Object]" at path "_id"',
    name: 'CastError',
    type: 'ObjectId',
    value: {},
    path: '_id' }

Could this be considered a bug in Mongoose?

Edit:

Mongoose is allowing {} to be saved to the above schema.

Example schema:

var schema = mongoose.Schema(mongoose.Schema{
    "refA": {type: mongoose.Schema.Types.ObjectId, ref: 'ReferenceA'},
    "refB": {type: mongoose.Schema.Types.ObjectId, ref: 'ReferenceB'},
    "refC": {type: mongoose.Schema.Types.ObjectId, ref: 'ReferenceC'}
});

Method handling PUT:

var id = /* existing document */,
    body = {"refA": {}};

query = Table.findByIdAndUpdate(id, {$set: body}).lean();
query.exec(function(err, record) { /* ... */ });

It's determined that Mongoose shouldn't allow {} to be saved as an ObjectId type. Haven't tested doing so when saving a new document / handling a POST.

Yann Eves
  • 337
  • 1
  • 10
  • 1
    It means your `ObjectId` is coming across as the string `[object Object]` – aarosil Feb 24 '15 at 21:11
  • 1
    Storing `{}` values where you're telling Mongoose to expect an ObjectID is definitely a bad idea. Figuring out how those are getting into your saved doc is the real problem to solve here. – JohnnyHK Feb 24 '15 at 21:12

0 Answers0