首先,请原谅我,这对我来说是一个非常未知的领域......
得到以下内容:
app.get('/user/:user_id/followings', function(req, res) {
var response = {}
, userId = req.params.user_id
, ids = req.param('id').split(',')
, prefix = 'user'
, suffix = 'follower_ids';
for (var i=0; i<ids.length; i++) {
var id = ids[i]
, key = prefix+':'+ids[i];
console.log('1: ' + key);
checkMembership(userId, id, key, suffix, function(error, reply){
response[key] = reply;
console.log('2: ' + key + ': ' + reply);
});
}
res.json(response);
});
function checkMembership(userId, id, key, suffix, callback) {
var lookup = key+':'+suffix;
client.sismember(lookup, userId, callback);
}
对于以下路径:/user/1/followings?id=1,2,3,4,1000
我在控制台中得到了这个:
1: user:1
1: user:2
1: user:3
1: user:4
1: user:1000
2: user:1000: 0
2: user:1000: 1
2: user:1000: 1
2: user:1000: 1
2: user:1000: 0
这在浏览器中:{}
我知道它正在发生,因为事情是异步发生的......我只是不确定如何解决它。
checkMembership 函数中记录的键是错误的。控制台应为第二个回调输出如下:
2: user:1: 0
2: user:2: 1
2: user:3: 1
2: user:4: 1
2: user:1000: 0
帮助!