Buckets
Buckets

Reputation: 89

Mongoose .findOne Error returns the model found?

So I'm trying to access this account by using the findOne function in mongoose, and I'm trying to console.log the error, but the error is just the correct model found.. once I find the correct model I want to access one of the nested objects in the schema so I can edit the value.

I'm not sure why this is happening, below I put the code as well as the error that was logged into the console, I can provide more if needed.

    let accountSchema = mongoose.Schema({
       username:{
           type: String,
           required: true,
           index: true,
           unique: true,
       },
       password:{
           type: String,
           required: true,
       },
       money:{
           type: Number,

       },
       inventory: { type: [{
           weed: { type: Number },
           coke: { type: Number },
       }]},
  });


mp.events.addCommand('coke', (player) => {
    console.log(player.name);
    Account.findOne({username: 'a'}, function(acc, err) {
        if(err) return console.log(err);
        console.log(acc.username);
       acc.inventory[1] = acc.inventory[1] + 1;
       acc.save(function(err){
          if(err) return player.outputChatBox('Not logged in');
          player.outputChatBox('Added 1 coke');
       });
    });
});

(Console) {"_id":"5b6acbbbc285477e39514cb9","username":"a","password":"$2a$10$XABqooqFRINYVdJ79.i2E.5xdpitRrfZxUBmIPAZjjaXKvvLDc2y2","money":5000,"inventory":[{"_id":"5b6acbbbc285477e39514cbb","weed":0},{"_id":"5b6acbbbc285477e39514cba","coke":0}],"__v":0}

Upvotes: 1

Views: 3665

Answers (2)

Cata John
Cata John

Reputation: 1401

The .findOne method callback must have the following parameters function (err, res). So you are setting them in a reversed order.

Check http://mongoosejs.com/docs/api.html#model_Model.findOne

Upvotes: 3

Tsvetan Ganev
Tsvetan Ganev

Reputation: 8856

The callback function for the .findOne method has the following signature:

function (err, obj) {

}

You are using the arguments in the wrong order - the error object is the first argument and the object found is the second one.

Upvotes: 6

Related Questions