'PromiseLike< void>类型中不存在属性'catch' [英] Property 'catch' does not exist on type 'PromiseLike<void>

查看:3281
本文介绍了'PromiseLike< void>类型中不存在属性'catch'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在以下代码中收到此错误。我正在使用ionic3:

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


类型'PromiseLike

这是教程

此错误显示在VS代码中。

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');
 })  
}

**这是home.ts文件的完整代码,它将令牌发送到firebase数据库:请参考这个同样,因为我还得到另一个错误:错误:未捕获(在承诺中):错误:当我删除catch块时,StaticInjectorError [AngularFireDatabase]。 **

**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');
  })  
  }

  }


推荐答案

push 返回 ThenableReference 而不是 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.

另见 codebase
ThenableReference Def 这里

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

或者您可以采用推荐的方式,即:
您目前无法使用catch。

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.

这篇关于'PromiseLike&lt; void&gt;类型中不存在属性'catch'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆