I returned mongoose docs as json in this way:
UserModel.find({}, function (err, users) { return res.end(JSON.stringify(users)); }
However, user.__proto__ was also returned. How can I return without it? I tried this but not worked:
UserModel.find({}, function (err, users) { return res.end(users.toJSON()); // has no method 'toJSON' }
To turn a Mongoose document into a plain object, we can use the lean method. MyModel. findOne(). lean().
Mongoose toObject and toJSON transform behavior with sub-documents. Published on Tuesday, June 7, 2016. Mongoose supports two Schema options to transform Objects after querying MongoDb: toObject and toJSON . In general you can access the returned object in the transform method toObject or toJSON as described in the ...
json() Function. The res. json() function sends a JSON response. This method sends a response (with the correct content-type) that is the parameter converted to a JSON string using the JSON.
You may also try mongoosejs's lean() :
UserModel.find().lean().exec(function (err, users) { return res.end(JSON.stringify(users)); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With