-3
rated 0 times
[
0]
[
3]
/ answers: 1 / hits: 12277
/ 10 Years ago, sat, june 21, 2014, 12:00:00
I use node.js on my server and I use redis key-store for storing data about my characters. Every connection has it own character. I want to get all data about characters(Person, has its name, age, profession, ...) into Characters array, so I can then selectively send it to connected clients.
var Characters = [];
for (var ID in Connections) {
redis_client.HGETALL(ID, function(err, result) {
if (result) {
Characters.push(result);
}
});
}
console.log(Characters);
I have read, that this is due to asynchronous vs synchronous problem, so I made global variable character.
//global variables
var character;
//function code
var Characters = [];
for (var ID in Connections) {
redis_client.HGETALL(ID, function(err, result) {
character = result;
});
if(character) {
console.log(character); // returns correct result
// make copy of character
Characters.push(JSON.parse(JSON.stringify(character)));
character = undefined;
}
}
console.log(Characters); // array of 1 character * number of connection
//BUT I expect different character for each connection
More From » node.js