我试图从我的redis数据库中获取所有hget值。但是数组日志保持空白。你们可以请看一下吗?感谢
getAllHMSets = (cb) ->
client.keys "log:*", (err, logKeys) ->
if not err and logKeys isnt null
logs = new Array()
i = 0
while i < logKeys.length
client.hgetall logKeys[i], (err, log) ->
if not err and log isnt null
logs.push log
i++
if logs.length is logKeys.length
cb logs if typeof cb is "function"
答案 0 :(得分:2)
我认为您的代码有两个问题:
你应该用更惯用的CoffeeScript
您对hgetall的调用也必须使用回调,否则您的数组永远不会被填充。
getAllHMSets = (cb) ->
client.keys "log:*", (err, logKeys) ->
if not err and logKeys isnt null
logs = []
for value in logKeys
client.hgetall logKeys[i], (err, log) ->
if not err and log isnt null
logs.push log
if logs.length is logKeys.length
cb logs if typeof cb is "function"