【问题标题】:nodejs driver for mongodb fndAndModify用于 mongodb findAndModify 的节点 js 驱动程序
【发布时间】:2015-08-20 09:41:04
【问题描述】:

我有以下代码:

var conditions = {_id: playerID, 'gameStats.GameID' : gameID},
    update = {$inc: {'gameStats.$.wins' : 1}},
    options = {new : true};

player.findAndModify(conditions, update, options, function (err, doc) {
    if (err) { 
        console.log("updated failed")
        return res.sendStatus(300);     
}

当我执行它时,返回错误消息“TypeError: undefined is not a function”,我真的不知道问题出在哪里。有人可以帮忙吗?

【问题讨论】:

  • player 来自哪里?您使用的是哪个版本的节点驱动程序?此外,我怀疑你的整个代码实际上受到"How to return the response from an asynchronous call" 的影响
  • Steven,感谢您的回复...不确定我使用的是什么版本的驱动器我有 Nodejs 版本 0.12.7...对于这一切仍然很新。玩家来自:mongoose.model('player', playerSchema);使用更新方法时它工作正常,但我改为 findAndModify 因为我需要使用 dotnation 才能访问子文档
  • 这会产生“巨大”的差异。不过,您仍然不应该在异步回调中调用 return。它实际上并没有做任何事情,因为它是最后一次调用。

标签: node.js mongodb mongoose driver findandmodify


【解决方案1】:

问题是您使用的是“mongoose”并且没有.findAndModify() 方法。请改用.findOneAndUpdate()

player.findOneAndUpdate(conditions, update, options, function (err, doc) {
    if (err) { 
        console.log("updated failed")
        res.sendStatus(300);     
    } else {
      // do things that are happy with the response
    }
});

【讨论】:

  • @Kevin Debono :MongoShell 命令类似于 NodeJs mongo 驱动程序的命令,但并不总是相同。我建议你看一下 Node Driver API Docs:mongodb.github.io/node-mongodb-native/2.0/api/…
  • @codename44 您没有向 OP 发送消息,因为他们没有在这里发表评论。它只出现在我身上。
猜你喜欢
  • 2014-08-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-18
  • 1970-01-01
  • 1970-01-01
  • 2016-03-13
相关资源
最近更新 更多