如何等待Firebase启动,避免错误:在React Native上"Client is offline"?



我需要帮助使用expo在React Native中检索firebase数据
当我刷新应用程序时,此代码运行良好,但当它首次启动时,会抛出一个错误:Error: Error: Client is offline.
也许我需要异步等待,我已经尝试了一些方法,但没有成功。

componentDidMount = async () => {
var radioFireData = null;
const { names } = this.props;
const dbRef = ref(db, "records/");
get(child(dbRef, "flap/"))
.then((snapshot) => {
if (snapshot.exists()) {
radioFireData = snapshot.val();
this.setState({ checked: radioFireData[names] });
} else {
console.log("No data available");
}
})
.catch((error) => {
console.log(error);
});
};

给…也许我可以帮助别人。

componentDidMount(){
const { names } = this.props;
const reference = ref(db, "records/" + "/flap/");
onValue(
reference,
(snapshot) => {
const data = snapshot.val();
this.setState({ checked: data[names] });
},
{
onlyOnce: true,
}
);
};

最新更新