node.js - return results from a function (javascript, nodejs) - Stack Overflow

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

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) {
    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;
            }
        }
        ));
    });
    return roomId;
}
sys.puts(routeToRoom("alex", "123"));

But I get always: 0

I guess return roomId; is executed before roomId=results.creationix.room. Could someone help me with this code?

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) {
    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;
            }
        }
        ));
    });
    return roomId;
}
sys.puts(routeToRoom("alex", "123"));

But I get always: 0

I guess return roomId; is executed before roomId=results.creationix.room. Could someone help me with this code?

Share Improve this question edited Nov 9, 2018 at 20:30 hong4rc 4,1034 gold badges23 silver badges42 bronze badges asked Aug 31, 2011 at 10:57 profesoralexprofesoralex 4171 gold badge3 silver badges10 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 44
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

You are trying to execute an asynchronous function in a synchronous way, which is unfortunately not possible in Javascript.

As you guessed correctly, the roomId=results.... is executed when the loading from the DB completes, which is done asynchronously, so AFTER the resto of your code is completed.

Look at this article, it talks about .insert and not .find, but the idea is the same : http://metaduck.com/01-asynchronous-iteration-patterns.html

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1737430044a3949187.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信