Cannot read property 'castForQuery' of und

2020-05-24 18:13发布

问题:

I am using arrayFilters as option in findOneAndUpdate (in mongoose) to update value of name and isRecursive fields in TimetableModel. The fields are updating successfully in MongoDb but also I am getting the exception, which is being added at the end of this query.

What am I supposed to do to eliminate this exception?

Following are the details of my work:

Versions: "mongodb": "^3.1.13", "mongoose": "^5.4.9",

this is the Schema:

const timetableSchema = new Schema({
individualId: {
    type: Schema.Types.ObjectId,
    required: true
},
date: {
    type: Number,
    required: false
},
time: []});

this is the logic:

TimetableModel.findOneAndUpdate({
        individualId: req.query.individualId,
        date: req.query.date,
    }, {
        'time.$[i].name': req.query.name,
        'time.$[i].isRecursive': req.query.isRecursive,
    }, {
        arrayFilters: [{
            'i.timeSlot': req.query.timeSlot
        }],
    }, function (err, result) {
        if (result) {
            resolve(result);
        } else if (err) {
            reject(err);
        }
    }).catch((err) => {
        reject(err);
    })

and this is the exception

"TypeError: Cannot read property 'castForQuery' of undefined at castArrayFilters (/Users/user/Desktop/Projects/ParentsPlus/ParentsPlusRepo/parents-plus-back-end/node_modules/mongoose/lib/helpers/update/castArrayFilters.js:59:37) at _castArrayFilters (/Users/user/Desktop/Projects/ParentsPlus/ParentsPlusRepo/parents-plus-back-end/node_modules/mongoose/lib/query.js:1739:5) at model.Query.Query._findAndModify (/Users/user/Desktop/Projects/ParentsPlus/ParentsPlusRepo/parents-plus-back-end/node_modules/mongoose/lib/query.js:3205:3) at model.Query. (/Users/user/Desktop/Projects/ParentsPlus/ParentsPlusRepo/parents-plus-back-end/node_modules/mongoose/lib/query.js:2830:8) at model.Query._wrappedThunk [as _findOneAndUpdate] (/Users/user/Desktop/Projects/ParentsPlus/ParentsPlusRepo/parents-plus-back-end/node_modules/mongoose/lib/helpers/query/wrapThunk.js:16:8) at process.nextTick (/Users/user/Desktop/Projects/ParentsPlus/ParentsPlusRepo/parents-plus-back-end/node_modules/kareem/index.js:369:33) at _combinedTickCallback (internal/process/next_tick.js:132:7) at process._tickCallback (internal/process/next_tick.js:181:9)"

回答1:

I think there is a bug since version 5.4.4 or maybe another previous version. It was reported here. For the moment I solved it by downloading the mongoose version to 5.3.15.