【问题标题】:node.js Redis GETnode.js Redis GET
【发布时间】:2012-08-04 17:46:42
【问题描述】:

我正在尝试从 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') 的值?

【问题讨论】:

    标签: node.js redis


    【解决方案1】:

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

    【讨论】:

    • +1 但还添加一个如何从 cdanea 使用的示例可能对操作有用。
    【解决方案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'}
        ]
    */
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-05-22
      • 1970-01-01
      • 2014-08-27
      • 2014-03-25
      • 2016-06-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多