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は、ラムダ関数を使用してコールバックを渡すことを目的としています。残りの決定を行動としてさまざまな応答に渡してみてください。

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 に答える