Object.assign not working as expected Object.assign not working as expected mongoose mongoose

Object.assign not working as expected


Mongoose documents (model instances) are special: they will only print properties that are present in the schema.

If properties aren't defined there, they won't show up when you console.log() them (and also not if you convert the document to a plain JS object with obj.toObject()).

This means that using Object.assign() will only work if you assign properties that are also present in the schema. Any properties that aren't declared will not be shown (nor saved to the database).

If your intention is to use the document for output, you should convert it to a proper JS object first before assigning to it:

let data = Object.assign(booking.toObject(), {  hiw   : event.hiw[booking.locale],  tip   : event.tip[booking.locale],  start : moment(event.start).format('L')});