Reputation: 4647
I have an existing key value list: key value1 value2
.
In redis-cli
, I run LRANGE key 0 -1
, which returns:
1) value1
2) value2
This confirms that the key value list exists. In redis-cli
, running LINDEX key 0
returns:
"value1"
However, in my node app, when I execute console.log(redis.lindex('key', 0))
, it prints true
rather than the value at the index.
What am I doing wrong?
Note: I'm using the node-redis
package.
Upvotes: 3
Views: 2702
Reputation: 42325
Calls to command functions in node-redis
are asynchronous, so they return their results in a callback and not directly from the function call. Your call to lindex
should look like this:
redis.lindex('key', 0, function(err, result) {
if (err) {
/* handle error */
} else {
console.log(result);
}
});
If you need to "return" result from whatever function you're in, you'll have to do that with a callback. Something like this:
function callLIndex(callback) {
/* ... do stuff ... */
redis.lindex('key', 0, function(err, result) {
// If you need to process the result before "returning" it, do that here
// Pass the result on to your callback
callback(err, result)
});
}
Which you'd call like this:
callLIndex(function(err, result) {
// Use result here
});
Upvotes: 9