1

In my Angular fullstack project I try to update a project adding a new sub-document to it. When checking my req.body before merging it all looks as it should. However after the merge the updated document has added a new sub-document as it should, the only problem is that this new document is a clone of an already existing one, instead of the one it should have been.

the _.merge is from lodash

the code:

    console.log('');
    console.log('notes before merging : ', entry.notes);

    console.log('');
    console.log('body.notes before merging : ', req.body.notes);

    var updated = _.merge(entry, req.body);

    console.log('');
    console.log('notes after merging : ', updated.notes); 


And my console result when running the code:

notes before merging :  
[
    { 
        content: '<p>testing</p>',
        date: Thu Apr 16 2015 10:14:44 GMT+0200 (Rom, sommertid),
        writer: 55193679026666b00554d00e,
        _id: 552f6f7435828478156f6103,
        notes: [] 
    }
]

body.notes before merging :  
[ 
    { 
        content: '<p>testing</p>',
        date: Thu Apr 16 2015 10:14:44 GMT+0200 (Rom, sommertid),
        writer: 55193679026666b00554d00e,
        _id: 552f6f7435828478156f6103,
        notes: [] 
    },
    { 
        content: '<p>bla bla</p>',
        date: '2015-04-16T08:25:24.431Z',
        writer: 55193679026666b00554d00e
    }
]

notes after merging :  
[
    { 
        content: '<p>testing</p>',
        date: Thu Apr 16 2015 10:14:44 GMT+0200 (Rom, sommertid),
        writer: 55193679026666b00554d00e,
        _id: 552f6f7435828478156f6103,
        notes: [] 
    },
    { 
        content: '<p>testing</p>',
        date: Thu Apr 16 2015 10:14:44 GMT+0200 (Rom, sommertid),
        writer: 55193679026666b00554d00e,
        _id: 552f6f7435828478156f6103,
        notes: [] 
    }
]
Michael Tot Korsgaard
  • 3,892
  • 11
  • 53
  • 89

1 Answers1

1

Try using _.extend or _.assign instead:

var updated = _.assign(entry, req.body);

This answer by ShitalShah highlights the differences between merge and extend that is causing duplicates in your resulting object with merge but essentially:

Here's how extend/assign works: For each property in source, copy its value as-is to destination. if property values themselves are objects, there is no recursive traversal of their properties. Entire object would be taken from source and set in to destination.

Here's how merge works: For each property in source, check if that property is object itself. If it is then go down recursively and try to map child object properties from source to destination. So essentially we merge object hierarchy from source to destination. While for extend/assign, it's simple one level copy of properties from source to destination.

JSBin to illustrate the differences:

var dest = {
  p: { x: 10, y: 20},
};

var src = {
  p: { x: 20, z: 30},
};

console.log(_.merge(dest, src)); 
/*
[object Object] {
  p: [object Object] {
    x: 20,
    y: 20,
    z: 30
  }
}
*/

console.log(_.extend(dest, src));
/*
[object Object] {
  p: [object Object] {
    x: 20,
    z: 30
  }
}
*/
Community
  • 1
  • 1
chridam
  • 100,957
  • 23
  • 236
  • 235