typescript - firebase thenable 参考返回处理
问题描述
我正在使用 angularfire2 并进行推送调用来存储对象。如下所示:
点击处理程序
assignCode(){
this.ddlSvc.assignCoupon(this.selItem.key, coupon).then(
(resp) => {
console.log("user created with:" + resp)
const alert = this.core.createAlert('Confirmation', 'Coupon Assgined to customer!')
alert.present()
}
)
}
调用firebase的角度服务
assignCoupon(key:string, coupon:Coupon){
return this.db.list('/users/' + key + '/coupons').push(coupon)
}
当调用客户端用户具有推送到该节点所需的权限时,它工作正常。但是,我们正在测试调用客户端没有权限的情况。目前,当我们触发这个案例时,我们会以一种非常丑陋的方式在 UI 上得到所有错误,而不是一个很好的弹出窗口,因为我们没有处理它。那么,我们如何处理错误部分呢?因为它是一个 thenable 引用,所以点击处理函数没有 . “.catch”来处理。
解决方案
如果您转到ThenableReference
(在 Visual Studio Code 中,按 Ctrl-T 然后键入ThenableReference
)的定义,您应该会看到如下内容:
interface ThenableReference
extends firebase.database.Reference,
PromiseLike<any> {}
我们关心的部分是PromiseLike<any>
. 如果你跳到那个定义,你应该看到这个:
interface PromiseLike<T> {
/**
* 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 completion of which ever callback is executed.
*/
then<TResult1 = T, TResult2 = never>(onfulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | undefined | null, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | undefined | null): PromiseLike<TResult1 | TResult2>;
}
请注意,它then
接受一个可选的第二个回调,在发生错误时调用。您应该能够使用它来代替该catch
方法。
推荐阅读
- python - 在 tkinter/python 中使用按钮时,有没有办法根据字典更改按钮文本?
- python - 我想配置一个文本编辑器(geany)来在我使用 puthon3.7 的 Windows 10 机器上执行文件
- java - 我应该怎么做才能在图像中查看复平面的一般矩形区域?
- sql - PostgreSQL 传递文件名作为参数
- javascript - 4 个唯一随机数的 JavaScript 数组 - 不起作用。为什么?
- python - 强化学习不适用于这个非常简单的游戏,为什么?Q学习
- java - 杀死并重新启动应用程序后前台服务没有停止 - Android Studio
- node.js - 节点的事件循环阶段回调
- python - Pandas Dataframe 未使用 .loc 返回列中 1 个字符串值的结果
- spring - 无法在 Eclipse 中启动 Spring Boot App:无法启动嵌入式 Tomcat