I have a problem with understanding the variable manipulation in JavaScript. Following code:
UserScore.find(filter, function (err, userScores) {
var contests = [];
userScores.forEach(function(userScore)
{
contests.push(userScore.ContestId);
});
Contest.find({ '_id': { $in : contests } }, function(err, contestItems)
{
var result = [];
contestItems.forEach(function(con)
{
userScores.forEach(function(element) {
if(element.ContestId == con._id)
{
con.UserTeamName = element.TeamName;
con.UserPersonalScore = element.Score;
console.log(con);
console.log(con.UserPersonalScore);
result.push(con);
return;
}
});
});
res.status(200).json(result);
});
});
prints "con" without the two added properties, and prints "con.UserPersonalScore" with the appropriate value. When pushed to result, con does not have additional properties. What am I missing?
I guess I am somehow creating local variables instead of properties, but why isn't it being pushed to the result array?