firebase thenable 参考 return 处理
firebase thenable reference return handling
我正在使用 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()
}
)
}
angular 调用 firebase 的服务
assignCoupon(key:string, coupon:Coupon){
return this.db.list('/users/' + key + '/coupons').push(coupon)
}
当调用客户端用户具有推送到此节点所需的权限时,它工作正常。但是,我们正在测试调用客户端没有权限的情况。目前,当我们触发这种情况时,我们会以一种非常丑陋的方式在 UI 上得到所有错误,而不是一个很好的弹出窗口,因为我们没有处理它。那么,我们如何处理错误部分呢?因为它是一个 thenable 引用,所以在点击处理函数上没有 . “.catch”来处理。
如果您转到 ThenableReference
的定义(在 Visual Studio 代码中,您将按 Ctrl-T,然后键入 ThenableReference
),您应该会看到类似 this:
interface ThenableReference
extends firebase.database.Reference,
PromiseLike<any> {}
我们关心的部分是PromiseLike<any>
。如果你跳转到定义,你应该看到 this:
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
方法。
我正在使用 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()
}
)
}
angular 调用 firebase 的服务
assignCoupon(key:string, coupon:Coupon){
return this.db.list('/users/' + key + '/coupons').push(coupon)
}
当调用客户端用户具有推送到此节点所需的权限时,它工作正常。但是,我们正在测试调用客户端没有权限的情况。目前,当我们触发这种情况时,我们会以一种非常丑陋的方式在 UI 上得到所有错误,而不是一个很好的弹出窗口,因为我们没有处理它。那么,我们如何处理错误部分呢?因为它是一个 thenable 引用,所以在点击处理函数上没有 . “.catch”来处理。
如果您转到 ThenableReference
的定义(在 Visual Studio 代码中,您将按 Ctrl-T,然后键入 ThenableReference
),您应该会看到类似 this:
interface ThenableReference
extends firebase.database.Reference,
PromiseLike<any> {}
我们关心的部分是PromiseLike<any>
。如果你跳转到定义,你应该看到 this:
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
方法。