【问题标题】:Authorization Error handling with FirebaseListObservable使用 FirebaseListObservable 处理授权错误
【发布时间】:2018-01-31 04:19:52
【问题描述】:

我正在尝试使用 catch 方法从 Firebase/AngularFire 捕获授权错误,但我收到错误 Property 'catch' does not exist on type 'FirebaseListObservable<any[]>'.

问题是当我无法访问基于身份验证规则的某些数据时。当我通过身份验证时,代码可以工作(没有 .catch,因为这是一个编译错误)。

Records: FirebaseListObservable<any[]>;

constructor(public afAuth: AngularFireAuth, public af: AngularFireDatabase) 
{
    try {
        this.user = afAuth.authState;
        this.Records = af.list('/Testing');
    }
    catch (e) {
        console.log((<Error>e).message);//conversion to Error type
    }
}

上述方法将起作用。但是,如果 /Testing 由于身份验证规则而无法访问,则会出现异常。我似乎无法用标准的 try/catch 捕捉到这个错误。不过,我确实看到了使用 catch 的参考,

        this.Records = af.list('/Testing').catch(e => {
            console.log((<Error>e).message);//conversion to Error type
        });

但是,这会导致上面的编译错误。

虽然我知道我可以更改 Firebase 读取权限以允许任何人读取数据,但我正在尝试处理此错误情况以使我的代码更安全,以防有人进入正在尝试的应用程序的一部分处理数据,但安全性不允许他们访问数据。

【问题讨论】:

    标签: typescript firebase angularfire2


    【解决方案1】:

    我不确定为什么 catch 不起作用,但您可以尝试这样的方法。 subscribe 的第二个参数是错误处理的参数,subscribe 必须存在于一个 observable 上。

    af.list('/Testing').subscribe(()=>{}, (err)=>{console.log(err)})
    

    【讨论】:

    • 这也会产生编译错误:Type 'Subscription' is not assignable to type 'FirebaseListObservable&lt;any[]&gt;'. Property '$ref' is missing in type 'Subscription'.
    • 嗯。我希望这能奏效。希望比我更有洞察力的人出现并帮助您,因为我不知道。对不起。
    • 没问题,谢谢。不过,你让我走在了正确的轨道上。我用它来找到另一个来源,表明我不应该将结果直接分配给 FirebaseListObject,而是将它们推入。这适用于直接数组,但不适用于结构化数据数组。我正在进一步调查。
    猜你喜欢
    • 2015-04-09
    • 2018-09-05
    • 2022-12-10
    • 2020-10-28
    • 2018-09-11
    • 2020-05-13
    • 2017-12-23
    • 1970-01-01
    • 2021-02-15
    相关资源
    最近更新 更多