gpt4 book ai didi

angular - 如何在 AngularFire/TypeScript 中访问 FirebaseError 的 "code"属性?

转载 作者:太空狗 更新时间:2023-10-29 18:07:17 26 4
gpt4 key购买 nike

FirebaseError has a "code" property ,但是你如何在 promise 的 catch 方法中读取它?以下抛出 TypeScript 错误:Property 'code' does not exist on type 'Error'.

this.af.database
.object(`/some/path`)
.set(newObj)
.then(data => {
console.log('success');
})
.catch(err => {
// Property 'code' does not exist on type 'Error'.
console.log(`code`, err.code);
});

最佳答案

要访问代码属性,您需要导入 firebase 并将您的错误指定为 firebase.FirebaseError 类型,如下所示:

import { AngularFire } from 'angularfire2';
import firebase from 'firebase';

...

constructor(
private af: AngularFire
) {}

...

this.af.database
.object(`/some/path`)
.set(newObj)
.then(data => {
console.log('success');
})
.catch( (err: firebase.FirebaseError) => {
// Give your error the firebase.FirebaseError type and
// you'll have access to all the FirebaseError properties
console.log(`code`, err.code);
console.log(`message`, err.message);
console.log(`name`, err.name);
console.log(`stack`, err.stack);
});

关于angular - 如何在 AngularFire/TypeScript 中访问 FirebaseError 的 "code"属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40157541/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com