0

我正在尝试从 Redis 数据库中获取值。代码:

                                    callback(null, 'Please enter PIN.');
                                    read = db.get(cmd + ':pin');
                                    console.log(read);
                                    n = db.get(cmd + ':name');
                                    waitingType = 'pin';
                                    wait = 1;

但是,当console.log(read)我得到true. 为什么我没有得到 的价值db.get(cmd + ':pin')

4

2 回答 2

2

Node 旨在使用 lambda 函数来传递回调。尝试将您的其余决定作为行为传递给各种响应:

read = db.get(cmd + ':pin', function(result) {
    console.log(read);
    // like this
    // ... and so on
});

此外,您可能想进一步了解 Redis,您可以一次检索所有字段。请参阅HMGET。我认为您可以改进存储数据的结构以更好地适应应用程序逻辑。

// for example:
// set like this, where id is some front stuff you know ahead of time, 
// like the expected username from a login form, that is expected to be unique
// you may concatenate and hash, just make this unique
db.hmset("authData:" + id, {id:'uniqID', pin:0666, name:'that guy'});

// get like this
db.hmget("authData:" + id, function(err, data) {
    console.log(['data will contain id, pin and name keys', data]);
});

//output:
/* [
    'data will contain id, pin and name keys', 
    {id:'uniqID', pin:0666, name:'that guy'}
    ]
*/
于 2014-02-12T23:04:02.017 回答
1

db.get 是异步的,因此当您的程序到达 console.log(read) 时,db 调用尚未完成

于 2012-08-07T16:58:43.080 回答