gpt4 book ai didi

node.js - Node 为 : hgetall returns true, 的 Redis 但回调返回 null

转载 作者:可可西里 更新时间:2023-11-01 11:47:16 24 4
gpt4 key购买 nike

redis 的新手,我找不到我做错了什么的答案。

我正在使用 Redis 将一些基本的分析信息存储为中间件:

module.exports = function (req, res, next) {
if (req.path !== '/login') {
const route = req.path
const ip = req.ip
const userId = req.userId
const timestamp = new Date()
client.hmset('HashKey', {
'route': route,
'ipAddress': ip,
'userId': userId,
'timestamp': timestamp
})
}

console.log(client.hgetall('Hashkey') + ' || Hashkey')

client.hgetall('Hashkey', function (err, results) {

if (err) {
return err
} else {
console.log(results + ' || Parsed Hashkey')
}
})
next()
}

console.log(client.hgetall('Hashkey') + ' || Hashkey') 返回 true,这应该表明数据库中有一个对象。但是 console.log(results + ' || Parsed Hashkey') 返回 null。

我是不是漏掉了什么?

编辑:这是初始化代码:

const redis = require('redis')
const client = redis.createClient({
port: 6379
})

最佳答案

redis库异步调用数据库。所以这个命令 console.log(client.hgetall('Hashkey') + ' || Hashkey') 不可能知道数据库中存在的对象,因为执行了 console.log 语句返回数据之前。

所以您的结果很可能是空的,因为您的数据库中还没有“HashKey”对象。

关于node.js - Node 为 : hgetall returns true, 的 Redis 但回调返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57578680/

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