return results from a function (javascript, nodejs)

后端 未结 2 419
情话喂你
情话喂你 2021-02-01 18:29

Could anyone help me with this code? I need to return a value form a routeToRoom function:

var sys = require(\'sys\');

function routeToRoom(userId, passw) {
            


        
2条回答
  •  醉话见心
    2021-02-01 19:26

    function routeToRoom(userId, passw, cb) {
        var roomId = 0;
        var nStore = require('nstore/lib/nstore').extend(require('nstore/lib/nstore/query')());
        var users = nStore.new('data/users.db', function() {
            users.find({
                user: userId,
                pass: passw
            }, function(err, results) {
                if (err) {
                    roomId = -1;
                } else {
                    roomId = results.creationix.room;
                }
                cb(roomId);
            });
        });
    }
    routeToRoom("alex", "123", function(id) {
        console.log(id);    
    });
    

    You need to use callbacks. That's how asynchronous IO works. Btw sys.puts is deprecated

提交回复
热议问题