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

荒凉一梦 提交于 2021-01-29 04:09:47

问题


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!


回答1:


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:

  • http://mongoosejs.com/docs/api.html#document_Document-update



回答2:


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.




回答3:


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) {});

`



来源:https://stackoverflow.com/questions/42933777/how-to-solve-typeerror-callback-apply-is-not-a-function

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