Add new element in existing object
Posted
by
user3094292
on Stack Overflow
See other posts from Stack Overflow
or by user3094292
Published on 2014-08-19T03:44:51Z
Indexed on
2014/08/19
4:20 UTC
Read the original article
Hit count: 115
I am using node.js.
I have to add new elements in the object before to send a response to client.
user.getMatch(req.user, function(err, match){
for( k=0; k<match.length; k++){
var userId = {
id : match[k].match_id
};
var user = new User(userId);
console.log('k: ' + k);
user.getUserInfo(function(err2, info){
console.log('k here: ' + k);
if(info){
match[k].foo = info[0].foo;
}
});
}
var response = {
data : match
};
res.json(response);
});
I want to add an element "foo" from user.getUserInfo to the object "match" that was returned by user.getMatch. And then send all the data as response to the client.
But it got an error because "k" inside of user.getUserInfo is not equal to the "k" outside. I do not know why the both "k" are not equal.
And how will I send a response to the client after performing the loop.
Thanks for your help!
© Stack Overflow or respective owner