0

I have the following code:

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

When I execute it the error message "TypeError: undefined is not a function" is returned and I really cannot figure out what is the problem. Can someone help?

Blakes Seven
  • 49,422
  • 14
  • 129
  • 135
  • 2
    Where is `player` coming from? And which version of the node driver are you using? Moreover, I supect your whole code actually suffers from ["How to return the response from an asynchronous call"](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call) – Blakes Seven Aug 20 '15 at 09:45
  • Steven, thanks for the reply...not sure what version of the drive I'm using bit I have Nodejs version 0.12.7...still very new to all this. player is coming from: mongoose.model('player', playerSchema); It was working fine when using the update method but I changed to findAndModify because I need to use the dotnation in order to access subdocuments – Kevin Debono Aug 20 '15 at 10:00
  • Well that makes a "huge" difference. You still should not be calling `return` within async callbacks though. It's not really doing anything since it's the last call anyway. – Blakes Seven Aug 20 '15 at 10:23

1 Answers1

1

The problem is that you are using "mongoose" and there is no .findAndModify() method. Use .findOneAndUpdate() instead:

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
    }
});
Blakes Seven
  • 49,422
  • 14
  • 129
  • 135
  • @Kevin Debono : MongoShell command's are similar to NodeJs mongo driver's ones, but not always identical. I suggest you to look at the Node Driver API Docs : http://mongodb.github.io/node-mongodb-native/2.0/api/Collection.html#findOneAndUpdate – codename44 Aug 20 '15 at 11:10
  • @codename44 You are not sending a message to the OP since they have not commented here. It only comes to me. – Blakes Seven Aug 20 '15 at 11:30