gpt4 book ai didi

node.js Redis 获取

转载 作者:可可西里 更新时间:2023-11-01 11:12:15 25 4
gpt4 key购买 nike

我正在尝试从 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 旨在使用 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'}
]
*/

关于node.js Redis 获取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11850587/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com