Typegoose: using async function in a pre hook

Created on 22 Sep 2020  路  6Comments  路  Source: typegoose/typegoose

@pre<User>('save', async function () {
  this.password = await Password.hash(this.password);
})
export class User {
  @prop()
  name!: string;

  @prop()
  password!: string;
}

Gives the following error:

Promise returned in function argument where a void return was expected.eslint(@typescript-eslint/no-misused-promises)

Is there a way to fix this? or it needs to be changed in the TypeScript typings?
https://github.com/typegoose/typegoose/blob/26afc6933e834b42a6ac02920658a240c6eda18a/src/hooks.ts#L14

+  type PreFnWithDocumentType<T> = (this: DocumentType<T>, next: HookNextErrorFn) => void | Promise<void>; 
-  type PreFnWithDocumentType<T> = (this: DocumentType<T>, next: HookNextErrorFn) => void; 

Versions

  • "@typegoose/typegoose": "^7.4.0"
  • "mongoose": "^5.10.6"
  • "typescript": "^4.0.3"
bug has repro script

All 6 comments

this definitely seems wrong, but i cant replicate it, could you provide an reproduction repo?

used code

Yes, sure.
https://github.com/kerolloz/typegoose-bug
Make sure ESLint is working in VSCode.
image

oh right forgot that, my project(s) dont use eslint (yet), so i couldnt reproduce it

will fix it

Thank you @hasezoey :heart_eyes:

will be included in 7.4.1

That's great! Thanks :sparkles:

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Negan1911 picture Negan1911  路  10Comments

wxs77577 picture wxs77577  路  5Comments

gserracalahorra picture gserracalahorra  路  6Comments

hmgibson23 picture hmgibson23  路  10Comments

dandv picture dandv  路  10Comments