I want to use post findOneAndUpdate middlewares to save a log datas of all my mongoose model changes.
I use findOneAndUpdate in three places:
- Update Model's data - change Model status - archive / restore Model
So when I use the post findOneAndUpdate middlewares, I want to Knwo wich one of the three findOneAndUpdate was executed.
Is there a way to pass parameters from findOneAndUpdate to post findOneAndUpdate middlewares.?
There is my Code:
controller: invoices.js
update: function (request, response, next) {
//checking general invoice information
request.checkBody(typeValidationSchema);
request.getValidationResult().then(function(result) {
if (!result.isEmpty()) return response.apiError(result.array());
else {
Invoice.findOneAndUpdate({
_id: request.params.invoiceId,
createdBy: request.user._id
}, request.body, {'new': true, 'runValidators': true}).exec(function (error, invoice) {
if (error) response.apiError(error);
if (!invoice) return response.apiNotFound();
else return response.apiResponse(invoice);
});
}
});
},
Model Schema: invoice.js
InvoiceSchema.post('findOneAndUpdate', function(doc) {
// I want to get her some variables passed from controller findOneAndUpdate
});