redis.lindex()返回true,而不是索引处的值
我有一个现有的键值列表: key value1 value2
。
在redis-cli
,我运行LRANGE key 0 -1
,它返回:
1) value1 2) value2
这证实存在关键值列表。 在redis-cli
,运行LINDEX key 0
返回:
"value1"
然而,在我的节点应用程序,当我执行console.log(redis.lindex('key', 0))
,它打印true
而不是索引值。
我究竟做错了什么?
注意:我正在使用node-redis
包。
在node-redis
中调用命令的function是asynchronous的,所以它们在callback中返回结果,而不是直接从函数调用中返回。 你打给lindex
电话应该是这样的:
redis.lindex('key', 0, function(err, result) { if (err) { /* handle error */ } else { console.log(result); } });
如果您需要从您所在的任何函数中“返回”结果,则必须使用callback来完成此操作。 像这样的东西:
function callLIndex(callback) { /* ... do stuff ... */ redis.lindex('key', 0, function(err, result) { // If you need to process the result before "returning" it, do that here // Pass the result on to your callback callback(err, result) }); }
你会这样叫:
callLIndex(function(err, result) { // Use result here });