【发布时间】:2019-05-31 13:43:26
【问题描述】:
我正在使用以下函数来更新 Firestore 中的数组:
component.ts
crearFavorito(key, e) {
this.fs.addFavorito(key);
this.snackBar.open(this.message, this.action, {
duration: 3000,
});
}
service.ts
addFavorito(key) {
const user = firebase.auth().currentUser;
this.afs.doc('eventos/' + key).update({
favoritos: firebase.firestore.FieldValue.arrayUnion(user.uid)
});
}
当用户通过身份验证时效果很好,但是当没有用户登录时,我在控制台中收到以下错误:
ERROR TypeError: Cannot read property 'uid' of null
鉴于此,我想做的是将其重定向到我的登录部分或显示必须验证的消息,然后我尝试了以下操作:
component.ts
crearFavorito(key, e) {
this.fs.addFavorito(key)
.then(() => {
this.snackBar.open(this.message, this.action, {
duration: 3000,
});
}, error => console.error('error:', error));
}
但我在then Typescript 中收到以下错误:
“void”类型上不存在“then”
知道如何实现吗?
【问题讨论】:
-
addFavorito没有return任何东西,所以对于 typescript,函数的类型是void。你需要回报一个承诺。 -
addFavorito不返回任何内容;目前尚不清楚您为什么期望得到不同的结果。 -
请阅读错误信息并在提问前使用
debugger;检查您的源代码。这确实有助于提高本网站上问题的质量。
标签: angular typescript google-cloud-firestore