Missing property at client side Nodejs Missing property at client side Nodejs mongoose mongoose

Missing property at client side Nodejs


The problem is that item is a MongooseDocument, not a plain javascript object.

There are multiple ways to achieve what you want.

1) Using .lean()

Documents returned from queries with the lean option enabled are plain javascript objects, not MongooseDocuments

Model.findOne().lean().exec((err, item) => {    item.mention = [{ id: 1 }];    res.json(item);});

This option will increase the performance since you ignore the overhead of creating the Mongoose Document

2) Using: .set()

item.set('mention', [{ id: 1}], { strict: false });

4) Using spread operator

res.json({    status: 1,    message: 'success',    data: { mention: [{ id: 5 }], ...item }})

4) Using Object.assign as @Alexis Facques explained.


Taking advantage of Object.assign() should resolve your problem too.

res.json({    status: 1,    message: 'success',    data: Object.assign({ mention: [{id: 1...}] },item)})