3

我正在尝试从我的 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"
4

1 回答 1

2

我认为您的代码有两个问题:

  1. 你应该用更惯用的 CoffeeScript 来写它

  2. 您对 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"            
于 2013-03-18T11:33:28.303 回答