Javascript Mongoose.findOne错误返回找到的模型?

Javascript Mongoose.findOne错误返回找到的模型?,javascript,node.js,mongodb,mongoose,Javascript,Node.js,Mongodb,Mongoose,因此,我尝试使用mongoose中的findOne函数访问此帐户,并尝试将错误记录在console.log中,但错误只是找到的正确模型。。找到正确的模型后,我希望访问模式中的一个嵌套对象,以便编辑该值 我不确定为什么会发生这种情况,下面我将代码以及登录到控制台的错误放在下面,如果需要,我可以提供更多 let accountSchema = mongoose.Schema({ username:{ type: String, req

因此,我尝试使用mongoose中的findOne函数访问此帐户,并尝试将错误记录在console.log中,但错误只是找到的正确模型。。找到正确的模型后,我希望访问模式中的一个嵌套对象,以便编辑该值

我不确定为什么会发生这种情况,下面我将代码以及登录到控制台的错误放在下面,如果需要,我可以提供更多

    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}

.findOne
方法的回调函数具有以下签名:

function (err, obj) {

}

您使用的参数顺序错误-错误对象是第一个参数,找到的对象是第二个参数。

方法的回调函数具有以下签名:

function (err, obj) {

}

您使用的参数顺序错误-错误对象是第一个参数,找到的对象是第二个参数。

findOne方法回调必须具有以下参数
函数(err,res)
。因此,您将按相反的顺序设置它们


选中。findOne方法回调必须具有以下参数
函数(err,res)
。因此,您将按相反的顺序设置它们


选中

将其添加到顶部。将其添加到顶部。感谢您的帮助这是一个常见错误。我也经历了很多次。这就是为什么我现在使用Promise API。谢谢你的帮助。这是一个常见的错误。我也经历了很多次。这就是为什么现在我使用Promise API。