Property 'catch' does not exist on type &#

2019-07-13 03:16发布

问题:

I'm getting this error in the following code. I'm using ionic3:

Property 'catch' does not exist on type 'PromiseLike

This is the link to the tutorial that I am following.

This error is showing in the VS Code.

This probably some updated syntax I'm not aware of

 storetoken(t) {
  this.afd.list(this.firestore).push({
  uid: firebase.auth().currentUser.uid,
  devtoken: t

}).then(() => {
  alert('Token stored');
  })
  .catch(() => {
    alert('Token not stored');
  })

this.afd.list(this.firemsg).push({
  sendername: 'vivek',
  message: 'hello'
}).then(() => {
  alert('Message stored');
  })
  .catch(() => {
    alert('Message not stored');
 })  
}

**This is the entire code for the home.ts file which sends token onto firebase database:Please refer this as well, as i'm also getting another error:Error: Uncaught (in promise): Error: StaticInjectorError[AngularFireDatabase] when I remove the catch block. **

 import { Component } from '@angular/core';
 import { NavController } from 'ionic-angular';
 import { AngularFireDatabase } from 'angularfire2/database';

 import firebase from 'firebase';
  import { HttpClientModule } from '@angular/common/http';
  import { HttpModule } from '@angular/http';

 declare var FCMPlugin;
 @Component({
   selector: 'page-home',
   templateUrl: 'home.html'
  })
 export class HomePage {
  firestore = firebase.database().ref('/pushtokens');
  firemsg = firebase.database().ref('/messages');
  constructor(public navCtrl: NavController, public afd: 
  AngularFireDatabase) {
  this.tokensetup().then((token) => {
  this.storetoken(token);
  })
  }

 ionViewDidLoad() {
 FCMPlugin.onNotification(function(data){
 if(data.wasTapped){
  //Notification was received on device tray and tapped by the user.
  alert( JSON.stringify(data) );
  }else{
  //Notification was received in foreground. Maybe the user needs to be 
   notified.
   alert( JSON.stringify(data) );
   }
  });

  FCMPlugin.onTokenRefresh(function(token){
  alert( token );
  });    
  }

  tokensetup() {
  var promise = new Promise((resolve, reject) => {
  FCMPlugin.getToken(function(token){
  resolve(token);
  }, (err) => {
    reject(err);
  });
  })
  return promise;
  }

  storetoken(t) {
  this.afd.list(this.firestore).push({
  uid: firebase.auth().currentUser.uid,
  devtoken: t

   }).then(() => {
   alert('Token stored');
   }).catch(() => {
    alert('Token not stored');
   })

  this.afd.list(this.firemsg).push({
  sendername: 'vivek',
  message: 'hello'
  }).then(() => {
  alert('Message stored');
  }).catch(() => {
    alert('Message not stored');
  })  
  }

  }

回答1:

push returns a ThenableReference and not a Promise.

Combined Promise and Reference; resolves when write is complete, but can be used immediately as the Reference to the child location.

It means you can use it as a future reference to the written data.

See also in codebase. ThenableReference Def here.

 export interface ThenableReference extends Reference, PromiseLike<Reference> {}

Or you can do the recommended way i.e: You cannot use catch currently.

 this.afd.list(this.firestore).push({ uid: firebase.auth().currentUser.uid, devtoken: t });

Note: There is an open issue here if you want to follow it.