mongoose required triggers error code example
Example 1: express unique validator for the owner
schmea.post('save', function(error, doc, next) {
if (error.name === 'MongoError' && error.code === 11000) {
next(new Error('email must be unique'));
} else {
next(error);
}
});
Example 2: how to avoid model , isNew , error from mongoose query
const documents = await DocumentModel.find().select({name:1}).lean()
// Use .lean() method before ending the queryy --> Happy coding