Post hooks
Post hooks
// user.model.js
const schema = new Schema({ username: { ... }, ... });
// add "post" save middleware
schema.post('save', function postSave(){
return new Promise((resolve, reject) => {
const email = this.email;
// ... do anything needed, maybe send an email?
if (someError) {
// If there is any error reject the Promise
return reject({ code: 500, message: 'Houston something went wrong.' });
}
});
});
// ....
// user.controller.js
const { instances } = require('gstore-node');
const User = require('./user.model');
const gstore = instances.get('default');
const user = new User({ name: 'John', email: 'john@snow.com' });
user.save().then((entity) => {
// You should do this check if you have post hooks that can fail
if (entity[gstore.ERR_HOOKS]) {
console.log(entity[gstore.ERR_HOOKS][0].message); // 'Houston something went wrong.'
}
...
});Transactions and Post Hooks
Last updated
Was this helpful?