Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't add new key to an object response from Mongoose

I have this problem and I can't figure why. inside a for loop I try to assign a new key value pair to each object in an array of objects, response from a mongoose query. e.g.

    obj = {
      value1: "someValue",
      value2: [],
      value3: {}
      value4: {
        id: "someId"
      }
   }

if I try to do a obj.value4.newKey = "newValue" nothing seems to happen, but the thing is that when I do

console.log(obj.value4.newKey) // prints "newValue"

but when I do

console.log(obj) 

or

console.log(obj.value4)

the new key added previously doesn't seems to exists

like image 551
flaalf Avatar asked May 28 '15 14:05

flaalf


1 Answers

I really don't know why, but I've found some kind of workaround, this it what worked (continuing with the first example)

res = JSON.parse(JSON.stringify(obj.value4));
res.newKey = "newValue";

now I do

console.log(res)

and I get

{
  id: "someId",
  newKey = "newValue"
}    

any ideas ?

like image 114
flaalf Avatar answered Sep 30 '22 05:09

flaalf