Mongoose Population: CastError: Cast to ObjectId failed for value “[object Object]” at path “_id”

梦想与她 提交于 2019-12-25 02:33:55

问题


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.

来源:https://stackoverflow.com/questions/28705810/mongoose-population-casterror-cast-to-objectid-failed-for-value-object-objec

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!