'PromiseLike<void> 类型上不存在属性 'catch' [英] Property 'catch' does not exist on type 'PromiseLike<void>
问题描述
我在以下代码中收到此错误.我正在使用 ionic3:
I'm getting this error in the following code. I'm using ionic3:
属性catch"不存在于类型PromiseLike"
这是我正在关注的教程的链接.
This is the link to the tutorial that I am following.
此错误显示在 VS Code 中.
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');
})
}
**这是将令牌发送到 firebase 数据库的 home.ts 文件的完整代码:也请参考这里,因为我还收到另一个错误:错误:未捕获(承诺):错误:StaticInjectorError[AngularFireDatabase] 当我删除 catch 块时.**
**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
.
push returns a ThenableReference
and not a Promise
.
结合Promise和Reference;写入完成时解析,但可以立即用作子位置的引用.
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.
另请参见代码库.ThenableReference Def 这里.
See also in codebase. ThenableReference Def here.
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<void> 类型上不存在属性 'catch'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!