admin管理员组

文章数量:1323529

I have the following code:

this.afDb.list('/demo').push({a: 'b'})
    .then(_ => console.log('works'))
    .catch(err => console.log('err: ', err));

I get the following error on 'catch':

[ts] Property 'catch' does not exist on type 'PromiseLike<any>'.

Seems like a AngularFire2 bug?

I have the following code:

this.afDb.list('/demo').push({a: 'b'})
    .then(_ => console.log('works'))
    .catch(err => console.log('err: ', err));

I get the following error on 'catch':

[ts] Property 'catch' does not exist on type 'PromiseLike<any>'.

Seems like a AngularFire2 bug?

Share Improve this question edited Jan 27, 2018 at 0:41 Frank van Puffelen 600k85 gold badges889 silver badges859 bronze badges asked Jan 27, 2018 at 0:13 flipcodeflipcode 6712 gold badges10 silver badges20 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 9

The error is reported because the PromiseLike interface in typescript lacks a catch method. This is consistent with the definition of a "thenable" as an object with a then method.

interface PromiseLike {
    /**
     * Attaches callbacks for the resolution and/or rejection of the Promise.
     * @param onfulfilled The callback to execute when the Promise is resolved.
     * @param onrejected The callback to execute when the Promise is rejected.
     * @returns A Promise for the pletion of which ever callback is executed.
     */
    then(onfulfilled?: ((value: T) => TResult1 | PromiseLike) | undefined | null, onrejected?: ((reason: any) => TResult2 | PromiseLike) | undefined | null): PromiseLike;
}

is how it's defined in lib.es2015.promise.d.ts on gitHub, starting at line 1291. (The linked HTML page is 5.5MB in size - it may take a while.)

Bear in mind too that the A5+ spec doesn't mention a catch method, and ES6 (ECMAScript 2015) promises provide catch as a Promise.prototype function that simply calls then with the first argument of catch as the second argument of then.

The easiest solution may be to use a two parameter then call:

this.afDb.list('/demo').push({a: 'b'})
    .then(_ => console.log('works'),
          err => console.log('err: ', err));

or expand the code for Promise.prototype.catch into a then call:

this.afDb.list('/demo').push({a: 'b'})
    .then(_ => console.log('works'))
    .then( null, err => console.log('err: ', err)); // catch

本文标签: javascriptProperty 39catch39 does not exist on type 39PromiseLikeltanygt39Stack Overflow