Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using ES6 Promises in Mongoose code

I am trying to use ES6 Promises for Mongoose ODM in TypeScrpt.

I have the following line

mongoose.Promise = Promise;

Which give me the following error:

error TS2322: Type 'PromiseConstructor' is not assignable to type 'typeof Promise'.
  Type 'Promise<any>' is not assignable to type 'Promise<any>'.
    Property 'end' is missing in type 'Promise<any>'.
like image 428
Ali Salehi Avatar asked Nov 22 '25 17:11

Ali Salehi


1 Answers

You are assigning the ES6 Native promise to mongoose promise:

mongoose.Promise = Promise;

You get the error

Property 'end' is missing in type 'Promise'.

Because the native Promise is missing the property end which is present in mongoose Promise (docs https://www.npmjs.com/package/mpromise).

Workaround

You can either:

  • Hack the mongoose definition to lie and say its a ES6 Promise instead of mpromise
  • Or Suppress the error:

    mongoose.Promise = Promise as any;
    
  • Or not do this entirely :)

like image 188
basarat Avatar answered Nov 25 '25 07:11

basarat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!