简体   繁体   中英

Invalid callback() argument error with Mongoose

I have a collection like this (very simplified)...

var parentSchema = new mongoose.Schema({
    firstName: String,
    mobile: String
});

var familySchema = new mongoose.Schema({
    groupId: { type: mongoose.Schema.Types.ObjectId, index: true },
    parents: [parentSchema]
});

For a given group, I'd like to find all of the parents of families in that group who have a mobile value set (exists), and unset those mobile values.

I've been able to piece this much together by looking at other examples...

Family.update(
    { groupId: someGroupId, "parents.mobile": {"$exists":"true"} },
    { $unset : { "parents.$.mobile" : 1 } }, false, true
).then(function() {
    // do other stuff
});

Running generates the error:

Trace: [Error: Invalid callback() argument.]

I've tried several variations, but this one seems the most correct to me.

The .update() signature for mongoose models is:

Model.update(<{query}>,<{update}>,[<{options}>],[callback])

So when using promises, it's just the first two with the optional "third" options . The "fourth" would be a callback function, and hence the error:

Family.update(
    { "groupId": someGroupId, "parents.mobile": {"$exists": true } },
    { "$unset": { "parents.$.mobile" : "" } },
    { "multi": true }
).then(function() {

Too many people read the "shell" signature , even though the usage of:

.update(<{query}>,<{update}>,<upsert>,<multi>)

Has been deprecated in favour of the standard "options" arrangement for some time.

Always refer to the method that actually applies to your language API.

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