You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi there. I'm not sure if I have run up against a limitation of this module, or of my skill. I can't seem to do anything with the return of a query outside of the optional function. The only way I can do anything to the data, is to put everything inside that function, i.e. passing the data as locals (res.render). Can you please set me straight?
For instance, I unable to attach the reply to a variable.
var data = client.get("test", function(err, reply){})
Similarly, I am unable to do this:
var data;
client.get("test", function(err, reply){
data = reply.toString()
}) // data undefined
Only this works, but it is not the ideal construction:
That's what you have to do with async functions in general, not just redis. There's a whole crop of modules (search for Flow Control / Async Goodies) dedicated to making async easier.
Your second example should work though, but you are probably getting confused about the order in which things run. Check out this program:
var client = require("redis").createClient();
client.set("some key", "some val");
var data;
client.get("some key", function (err, res) {
data = res;
console.log("Inside callback, data is: " + data);
});
console.log("Outside callback, data is: " + data);
This will output:
Outside callback, data is: undefined
Inside callback, data is: some val
Note that the "Outside callback" ran first. Since client.get() is a potentially blocking operation, the callback is noted, and control passes immediately (as far as you are concerned) to the next line in your program.
Note also that client.set() and client.get() are both operations that complete asynchronously, but they are guaranteed to complete in order. So you know that the get will not invoke the callback until the set is complete.
Hi there. I'm not sure if I have run up against a limitation of this module, or of my skill. I can't seem to do anything with the return of a query outside of the optional function. The only way I can do anything to the data, is to put everything inside that function, i.e. passing the data as locals (res.render). Can you please set me straight?
For instance, I unable to attach the reply to a variable.
Similarly, I am unable to do this:
Only this works, but it is not the ideal construction:
The text was updated successfully, but these errors were encountered: