【问题标题】:How to solve "TypeError: callback.apply is not a function"?如何解决“TypeError:callback.apply 不是函数”?
【发布时间】:2018-05-29 03:43:32
【问题描述】:

我正在做一个大学项目,我已经阅读了所有关于我的问题的帖子,但我还没有找到解决方案。也许你能帮帮我。

代码如下:

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

我收到以下错误。我看不出我做错了什么。

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

提前谢谢你!

【问题讨论】:

  • update 方法必须遵循参数:update(doc, options, callback)。你传递了一个额外的对象。
  • 我对 mongodb 一无所知,但我查看了update() 的一些文档,看来您使用它确实不正确。那么,您是否查看了您正在使用的方法的文档?

标签: javascript node.js mongodb mongoose


【解决方案1】:

您使用了太多参数。

改变这个:

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

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

到这里:

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

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

查看文档:

【讨论】:

    【解决方案2】:

    如果您要更新 mongoose 文档,则不要将查询作为第一个参数传入。

    请参阅Docs for Document#update

    因此,此更新方法需要 3 个参数 第三个是回调,你传入一个对象({upsert: true}),更新方法需要回调。这就是你得到callback.apply is not a function 的原因。仅仅因为{ upsert: true } 不是一个函数。

    【讨论】:

      【解决方案3】:

      就我而言,我在使用 mongoose (5.1.2) 的聚合方法时遇到了这个问题。相同的代码在

      我刚刚在查询中添加了大括号

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

      `

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-11-18
        • 2019-05-19
        • 2019-10-31
        • 2020-06-21
        • 2017-08-26
        • 1970-01-01
        • 2021-06-21
        • 1970-01-01
        相关资源
        最近更新 更多