3

I'm trying to get all hget values out of my redis db. But the array logs stays empty. Could you guys please take a look into it? Thanks

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"
2
  • 2
    Could it be that hgetall is executed asynchronously (that would explain the use of a callback)? Commented Mar 17, 2013 at 23:30
  • Thanks, Thilo. HGETALL is async, I'm using redis.multi() now which solves the issue ;) Commented Mar 17, 2013 at 23:59

1 Answer 1

2

I think there are 2 problems with your code:

  1. you should write it in more idiomatic CoffeeScript

  2. your call to hgetall must also use a callback eitherwise your array will never be filled.

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"            
Sign up to request clarification or add additional context in comments.

6 Comments

either your indentation is busted or you miss a return
oops! where do I miss a return?
if not err and logKeys isnt null
sorry, I dont get it. Why would there be a return missing? What should a return at this position do?
your if here is basically useless since you don't have anything in it. Actually coffee will refuse to parse it since it's not a post if (there's nothing before) and it has no block
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.