【发布时间】:2019-12-18 09:08:57
【问题描述】:
我正在尝试从 firebase 读取数据。但是代码是异步工作的,它会在获取数据之前显示数据。
我已经发布了我的代码
componentDidMount(){
var ref2 = database.ref();
this.getItems(ref2).then((items) => {
this.getResult(items).then((val) => {
// why is this val coming empty
ToastAndroid.show(JSON.stringify(val), ToastAndroid.SHORT);
});
});
}
getItems = async (ref2) => {
var items = [];
await ref2.once('value', snap => {
snap.forEach((child) => {
items.push(child.key);
});
});
// this is working fine . items is correctly returned
return items;
}
getResult = async (items) => {
var response = await this.getFinal(items);
// this response is empty why
return response;
}
getFinal = async (items) => {
var result = [];
await items.forEach((key) => {
ref = database.ref(key);
ref.once('value', snap => {
snap.forEach((child) => {
//val.push(child.val());
var id = child.key;
var data = {
username: child.val().username,
email: child.val().email,
name: child.val().name
};
result.push({[id]:data});
});
});
});
// this result is empty why
return result;
}
我希望 val 不为空,结果也不为空
【问题讨论】:
-
你在方法
getFinal中调用await items.forEach...。[Array].forEach不是异步的。另见:stackoverflow.com/a/5050317/2692307 -
非常感谢您指出这一点。我真的忽略了这部分
-
您还在等待
.once()。.on()和.once()模式在 EventEmitters 上注册事件处理程序——它们不返回 Promise。 Promise 和 EventEmitters 是处理异步程序流的两种不同设计模式。因此await不适用于.once()
标签: javascript react-native firebase-realtime-database async-await