简体   繁体   中英

Cannot read property 'castForQuery' of undefined at castArrayFilters in Node.js

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) a t _combinedTickCallback (internal/process/next_tick.js:132:7) at process._tickCallback (internal/process/next_tick.js:181:9)"

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM