14

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?

Blakes Seven
  • 49,422
  • 14
  • 129
  • 135
Emilia Tyl
  • 565
  • 1
  • 5
  • 17
  • You need to provide a test case that actually demonstrates the problem — http://sscce.org/ — you're manipulating a lot of variables (this can probably be simplified), but not showing us how they are defined, and you're making claims about what the value of `con` in `result` is without showing us how you are determining that. – Quentin Jul 21 '15 at 09:00
  • Please writer complete json or object structure of con – Shubham Nigam Jul 21 '15 at 09:00
  • 3
    There doesn't seem to be any JSON here either. http://benalman.com/news/2010/03/theres-no-such-thing-as-a-json/ – Quentin Jul 21 '15 at 09:01
  • Sorry, I pasted wrong snippet, that "this" was just an experiment. xd – Emilia Tyl Jul 21 '15 at 09:03
  • @BarthZalewski that does not help. – Emilia Tyl Jul 21 '15 at 09:04
  • @Quentin I edited my question and inserted a wider part of code, hope it helps. – Emilia Tyl Jul 21 '15 at 09:07
  • This has nothing to do with JSON whatsoever, except that the end result is translated to JSON. – RemcoGerlich Jul 21 '15 at 09:09
  • @RemcoGerlich I thought that JSONs are returned from the database (MongoDB) – Emilia Tyl Jul 21 '15 at 09:11
  • 2
    The object read from the MongoDB is supposed to be in frozen state. You might not be able to mutate it. – TaoPR Jul 21 '15 at 09:34
  • 1
    @TaoP.R. That is right! Thanks a lot! Thanks to your comment I found this question: [Why can't you modify the data returned by a Mongoose Query](http://stackoverflow.com/questions/14504385/why-cant-you-modify-the-data-returned-by-a-mongoose-query-ex-findbyid) and using `lean()` did the trick. Can you post an answer so that I could accept it? – Emilia Tyl Jul 21 '15 at 09:40

1 Answers1

34

Object returned from Mongodb query is in frozen (immutable) state

Your code seems to interact with MongoDB. The object returned is actually a Mongodb model instance, not a plain javascript object. You can't modify the object returned from a query.

To convert the Mongodb document to a JSON object

.toObject() does the trick. It converts a frozen MongoDB document to a JSON object.

TaoPR
  • 5,932
  • 3
  • 25
  • 35
  • 2
    Workaround: [How to get a plain JS object that can be manipulated](http://stackoverflow.com/questions/14504385/why-cant-you-modify-the-data-returned-by-a-mongoose-query-ex-findbyid) . – Emilia Tyl Jul 21 '15 at 09:47
  • 1
    I also update it with a reference to `.toObject()` since nobody mentioned it in the thread yet. Hope it's helpful to people who came across this too. – TaoPR Jul 21 '15 at 09:51
  • 1
    *to a JS object. JSON is a string structure. – iuliu.net Dec 07 '18 at 14:49