我有一个案例需要获取所有event
个对象。每个event
对象都有一个属性city_id
,它是列表中包含所有city
的单个cities
对象的关系键。
...
|- events
| |- event_id
| | |- title
| | |- city_id
|
|- cities
| |- city_id
| | |- name
| | |- location
...
所以我做了类似join
查询的事情:
const rootRef = firebase.database().ref();
async function getAllEvents() {
const eventsRef = rootRef.child('events');
const eventsArray = [];
await eventsRef.on('child_added', async (snapshot) => {
const eventObject = snapshot.val();
const cityObject = await getEventCity(eventObject.city_id);
eventObject.city = cityObject;
eventsArray.psuh(eventObject);
});
return eventsArray;
}
function getEventCity(cityId) {
return rootRef.child('cities')
.child(cityId)
.once('value', (venue) => {
return venue.val();
});
}
一切都适用于查询。问题是我想要获取数组中的所有事件,但以下代码不起作用:
const allEvents = getAllEvents();
我await
之前eventsRef.on('child_added)
的事件仍会返回初始的空数组。
我做错了什么,如何在.on('child_added')
上取得所有内容时抓住这一时刻?
答案 0 :(得分:2)
了解何时加载初始数据的唯一方法是使用43.394 - Start
45.275 - Request: 60
45.277 - 1: R60
45.277 - 2: R60
45.278 - 3: R60
45.278 - 4: R60
45.278 - 5: R60
45.278 - 6: R60
事件。我还没有真正使用value
/ async
,但预计你需要这样的东西:
await