node_redis - embarrassingly easy question

57 views
Skip to first unread message

Aaron Reabow

unread,
Jun 8, 2015, 1:11:10 PM6/8/15
to nod...@googlegroups.com
I have the feeling this might be really dumb.  I am trying to obtain the value of a list's length from redis.

It is returning correctly inside the anonymous function (if I console.log it), I just can't for the life of me seem to access outside of that function.

Here I have set a local variable - len - and tried to assign a value for it inside the function.

I wouldn't expect it to work like this, because I should set a global variable len instead.

The strange thing is that neither approach works.  Even if the global variable did the trick, it seems remiss to be using a global variable for something so trivial.

Any ideas on a solution?  Here is my code:

var len = 0
redis.llen("chatRecord", function (err, length) {
            log('length from llen is: ' + length)
len = length
        });

Aaron Reabow

unread,
Jun 9, 2015, 9:42:41 AM6/9/15
to nod...@googlegroups.com
Right.  So I forgot about non blocking.  The call to redis will enough time for js to move on and execute down the page.

I had the feeling this was going to be embarrassing :)

Christopher Mina

unread,
Jun 9, 2015, 9:42:52 AM6/9/15
to nod...@googlegroups.com
At what point outside of the redis callback are you checking the value of "len"? As this, like most calls in JS, is async, you must wait for the call to complete and then notify your calling method via a callback function, watcher system, event system, etc. What you can't do is simply reference the Len value beneath your redis call and expect it to be set.

Good luck
Chris

Adam Reynolds

unread,
Jun 9, 2015, 9:52:57 AM6/9/15
to nod...@googlegroups.com
Have a look at Promises or Observables. 


--
Job board: http://jobs.nodejs.org/
New group rules: https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
---
You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+un...@googlegroups.com.
To post to this group, send email to nod...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nodejs/aed69e5f-1c54-408d-a425-4664b99ee1ab%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Christopher Mina

unread,
Jun 9, 2015, 9:52:57 AM6/9/15
to nod...@googlegroups.com
It happens! Glad to see people involved in JS at any level. 

Chris
--
You received this message because you are subscribed to a topic in the Google Groups "nodejs" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/nodejs/nqYMwlBXzcU/unsubscribe.
To unsubscribe from this group and all its topics, send an email to nodejs+un...@googlegroups.com.

To post to this group, send email to nod...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.


--
Christopher Mina

Live Healthy. Work Hard. Be Kind. Gain Success.

Reply all
Reply to author
Forward
0 new messages