node.js socket.io socket.set() undefined property

2019-08-18 02:49发布

I have something like this. This is my Controller.

    new singleton('User', function(model){
    model.find({username:data.username, password:_pass}, function(err,user){
        if (user.length==1)
        {
            socket.set('taxiId',user[0].id);
            cb(true);
        }
        else
            cb(false);
    });
});

If I set socket property like socket.set('name',value, function(){cb});

I get undefined here

        auth.authentificate(data, socket, function(logged){
        if (logged)
        {
            logs.login(data,socket);
            console.log(socket.taxiId);
            socket.emit('authOk',{err:'Ziaden error'});
        }

I tried set it with this

socket.set('taxiId', user[0].id, function(){cb(true);});

But nothing happend

Otherwise if I set property as

socket.taxiId = user[0].id;

It works simply good.

But here is one problem in asynchrone coding. If one setter socket.taxiId = 1; is called second getter socket.taxiId; returning undefined.

0条回答
登录 后发表回答