Array.push,数组保持为空

时间:2013-03-17 23:29:24

标签: node.js coffeescript redis

我试图从我的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"

1 个答案:

答案 0 :(得分:2)

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

  1. 你应该用更惯用的CoffeeScript

  2. 来编写它
  3. 您对hgetall的调用也必须使用回调,否则您的数组永远不会被填充。

  4. 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"