admin管理员组

文章数量:1401143

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}

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}
Share Improve this question edited Aug 8, 2018 at 11:26 Buckets asked Aug 8, 2018 at 11:19 BucketsBuckets 891 silver badge10 bronze badges 1
  • Added it to the top. – Buckets Commented Aug 8, 2018 at 11:26
Add a ment  | 

2 Answers 2

Reset to default 6

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.

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

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

本文标签: javascriptMongoose findOne Error returns the model foundStack Overflow