简体   繁体   中英

How to solve “TypeError: callback.apply is not a function”?

I am doing a university project and I've read every post regarding my problem, but I am yet to find a solution. Maybe you can help me out.

The code is the following:

 viewerObj.update({_id: currentIDViewerVar} , {minutesWatched: 5},{upsert:true} , function (err,result) { if (err) throw err; console.log("Viewer " + userNameVar + " gespeichert"); console.log("minsWatched" +minsWatched); }); 

I get the following error. I can't see what I am doing wrong.

 events.js:160 throw er; // Unhandled 'error' event ^ TypeError: callback.apply is not a function at C:\\Users\\picco\\Desktop\\TwitchWatcher_v19\\TwitchWatcher\\node_modules\\mongoose\\lib\\model.js:3388:16 at Query.callback (C:\\Users\\picco\\Desktop\\TwitchWatcher_v19\\TwitchWatcher\\node_modules\\mongoose\\lib\\query.js:2185:9) at C:\\Users\\picco\\Desktop\\TwitchWatcher_v19\\TwitchWatcher\\node_modules\\mongoose\\node_modules\\kareem\\index.js:259:21 at C:\\Users\\picco\\Desktop\\TwitchWatcher_v19\\TwitchWatcher\\node_modules\\mongoose\\node_modules\\kareem\\index.js:127:16 at _combinedTickCallback (internal/process/next_tick.js:67:7) at process._tickCallback (internal/process/next_tick.js:98:9) Process finished with exit code 1 

Thank you in advance!

You're using too many arguments.

Change this:

viewerObj.update({_id: currentIDViewerVar} , {minutesWatched: 5},{upsert:true}  , function (err,result) {

      if (err) throw err;
      console.log("Viewer " + userNameVar + " gespeichert");
      console.log("minsWatched" +minsWatched);
});

to this:

viewerObj.update({_id: currentIDViewerVar, minutesWatched: 5}, {upsert:true}, function (err,result) {

      if (err) throw err;
      console.log("Viewer " + userNameVar + " gespeichert");
      console.log("minsWatched" +minsWatched);
});

See the docs:

If you're updating a mongoose document, you don't pass in a query as the first parameter.

see the Docs for Document#update .

Thus, this update method expects 3 parameters with the third being the callback and you pass in an object ( {upsert: true} ) where the update method expects the callback. That's why you get callback.apply is not a function . Simply because { upsert: true } is not a function.

In my case, I was facing this issue with aggregate method of mongoose (5.1.2). The same code was working on <4.9 but got broken on upgrading to 5.

I just added capital braces in my query

User.aggregate([{
            $match: {
                isDeleted: 0,
                isActive: 1,
                _id: Mongoose.Types.ObjectId(userId)
            }
        }, {

            $project: {
                claps: 1,
                showIcon: { $cond: [{ $gt: [{ $size: "$userBadges" }, 0] }, 1, 0] },
            }
        }])
            .exec(function (err, data) {});

`

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