Skip to content

Instantly share code, notes, and snippets.

@iamdanthedev
Created December 19, 2017 14:21
Show Gist options
  • Save iamdanthedev/85ee5aa124ca0227cd3a02650d7143f6 to your computer and use it in GitHub Desktop.
Save iamdanthedev/85ee5aa124ca0227cd3a02650d7143f6 to your computer and use it in GitHub Desktop.
Mongoose post init hook in typescript
// @ts-ignore
nutrientSchema.post('init', async function(this: NutrientInstance) {
// noinspection JSDeprecatedSymbols
if (!this.displayUnitCode && this.displayUnitId) {
const unit = await NutrientUnitModel.findById(this.displayUnitId);
if (unit) {
this.displayUnitCode = unit.code;
await this.save();
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment