Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/462.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用从MongoDB获取的新数据更新对象属性_Javascript_Node.js_Mongodb - Fatal编程技术网

Javascript 使用从MongoDB获取的新数据更新对象属性

Javascript 使用从MongoDB获取的新数据更新对象属性,javascript,node.js,mongodb,Javascript,Node.js,Mongodb,我正在尝试使用从MongoDB获取的新数据更新对象属性。以下代码无效。我不知道为什么?有人能帮我吗 var UserDB = require('../models/user_model.js').UserModel; function User(uid, username, credit, socket, seat, normal, bank, bankThree) { this.uid = uid; this.username

我正在尝试使用从MongoDB获取的新数据更新对象属性。以下代码无效。我不知道为什么?有人能帮我吗

 var UserDB = require('../models/user_model.js').UserModel;    
    function User(uid, username, credit, socket, seat, normal, bank, bankThree) {
            this.uid = uid;
            this.username = username;
            this.credit = credit;
            this.socket = socket;
            this.seat = seat;
            this.normal = normal;
            this.bank = bank;
            this.bankThree = bankThree;
            this.serverPerform = 0;
            this.actionTaken = false;
            this.cards = [];
            this.bet = 0;
            this.catched = false;
            this.action = false;
        }
        User.prototype.update = function(){
            var self = this;
            UserDB.findOne({uid:this.uid},function(err,doc){
                console.log(doc);
                if(!err){
                    self.username = doc.username;
                    self.credit = doc.credit; 
                    console.log(self); // work as expected
                }
            });

            console.log(self); // nothing change
        };
    var user = new User(1);
    user.update(); //nothing change

看起来您的mongo更新函数是异步的,因此回调内的日志当然可以工作,而回调外的日志则不能

User.prototype.update = function(callback){ //Now we've got a callback being passed!
    var self = this;
    UserDB.findOne({uid:this.uid},function(err,doc){
        console.log(doc);
        if(!err){
            self.username = doc.username;
            self.credit = doc.credit; 
            console.log(self); // work as expected
            callback(); //pass data in if you need
        }
    });
};
现在您已经在中传递了一个
回调
,您可以这样使用它:

var user = new User(1);
user.update(function() {
    //do stuff with an anonymous func
});
或者定义回调并将其传入

function callback() {
    //do stuff
}

var user = new User(1);
user.update(callback);

谢谢,你真是天才。