问题
I have a node.js (express) application and I'm using node_redis to get all users from my redis db.
redis = require "redis"
client = redis.createClient()
client.smembers "users", (err, user_ids) ->
results = new Array()
for user_id in user_ids
client.hgetall user_id, (err, items) ->
results.push items
if user_ids.length is results.length
res.json results
This produces the following result:
[
{
"name": "user1",
"password": "secret"
},
{
"name": "user2",
"password": "secret"
}
]
Now I want the user_id to be added to the user result, so I can get the following output:
[
{
"user:1": {
"name": "user1",
"password": "secret"
}
},
{
"user:2": {
"name": "user2",
"password": "secret"
}
}
]
The problem I have is that client.hgetall() is called asynchronously and I can't simply access the user_id in the for loop.
回答1:
You need to use a closure to store user_id by introducing a function in the loop. One way to do it is to use the forEach function to iterate on the array.
Here is an example in Javascript:
var redis = require('redis')
var client = redis.createClient();
function fetch( callback ) {
var results = new Array();
client.smembers( "users", function(err,users) {
if ( users.length == 0 )
return callback( results );
users.forEach( function(id) {
client.hgetall(id, function(err,items) {
var obj = {};
obj[id] = items; # here id can be accessed since it is part of the closure
results.push(obj);
if ( results.length == users.length ) {
callback( results );
}
});
});
});
}
fetch( function(results) {
console.log(JSON.stringify(results));
});
The output is:
[ {"user:2":{"name":"user2","password":"secret2"}},
{"user:1":{"name":"user1","password":"secret"}} ]
来源:https://stackoverflow.com/questions/12783683/how-can-i-access-the-key-passed-to-hgetall-when-using-node-redis