I wrote the code below, trying to collect the videos in an array then return. The code is wrong But I can't figure out the right way to do this.
var redis = require('redis');
var client = redis.createClient();
app.get('/topvideos', function(req, res){
res.type('application/json');
var topvideos = [];
client.hkeys("topvideos", function(err,replies) {
console.log("Results for video:");
console.log(replies.length + " videos:");
replies.forEach(function (reply, i) {
client.hget("topvideos",i, function (err, reply) {
console.log(i + ": " + reply );
topvideos.push(reply);
});
});
}
var string = JSON.stringify(topvideos)
res.send(string);
});
Is there an elegant pattern I could follow?