firebase - React-native-firebase 数据库 - 从嵌套调用中检索数据
问题描述
我有一个这样的 Firebase 数据库(例如):
{
'itemCategories': [
_categoryKey1: {
name: 'Category 1'
},
_categoryKey2: {
name: 'Category 2'
},
_categoryKey3: {
name: 'Category 3'
},
],
'items': [
_itemKey1: {
categoryId: '_categoryKey1',
name: 'Item 1',
}
_itemKey2: {
categoryId: '_categoryKey2',
name: 'Item 2',
}
_itemKey3: {
categoryId: '_categoryKey1',
name: 'Item 3',
}
_itemKey4: {
categoryId: '_categoryKey3',
name: 'Item 4',
}
_itemKey5: {
categoryId: '_categoryKey3',
name: 'Item 5',
}
]
}
我想要得到的是按类别分组的一组项目,例如:
itemList = [
{
category: 'Category 1',
items: [
{
id: '_itemKey1',
categoryId: '_categoryKey1',
name: 'Item 1',
},
{
id: '_itemKey3',
categoryId: '_categoryKey1',
name: 'Item 3',
}
]
},
{
category: 'Category 2',
items: [
{
id: '_itemKey2',
categoryId: '_categoryKey2',
name: 'Item 2',
}
]
},
{
category: 'Category 3',
items: [
{
id: '_itemKey4',
categoryId: '_categoryKey3',
name: 'Item 4',
},
{
id: '_itemKey5',
categoryId: '_categoryKey3',
name: 'Item 5',
},
]
},
]
我使用一个react-redux
动作来实现这一点,如下所示:
export const setItemList = (list) => {
return {
type: 'setItemList',
value: list
}
}
export const getItemListByCategory = () => {
return (dispatch) => {
let itemList = []
firebase.database().ref('itemCategories').on('value', (snapCategory) => {
snapCategory.forEach((category) => {
const categoryId = category.key
firebase.database().ref('items').orderByChild('categoryId').equalTo(categoryId).on('value', (snapItem) => {
let items = []
snapItem.forEach((item) => {
items.push({
id: item.key,
...item.val()
})
})
itemList.push({
category: category.val().name,
items: items
})
// This is where I think is the issue
dispatch(setItemList(itemList))
})
})
// The dispatch should be there, when every calls resolved
})
}
}
在大多数情况下,这工作得很好。当我处于forEach
循环中时,我将按类别检索数据,并实际调度多个部分 itemList
,直到forEach
循环结束。显然,我宁愿等到循环结束后再分派完整的itemList
.
我不知道如何等待循环结束 - 以及所有要解决的调用。我觉得我应该一起工作,Promise
但我不知道如何实现它。
解决方案
使用 Promise.all() 怎么样?firebase.database().ref('items').orderByChild('categoryId').equalTo(categoryId).on
您可以将此调用添加到 promises 数组,而不是触发调度。像这样的东西:
const promises = [];
snapCategory.foreach(snap => {
promises.push(your calls here, instead of dispatch return the list here)
})
Promise.all(promises).then((...args) => {
// args should contain all lists here, you can manipulate them as you need
// and dispatch afterwards
})
推荐阅读
- javascript - 如何与 Django 中的生物指纹识别器进行通信
- javascript - 在包含对象数组的对象内定义一个方法(javascript)
- mysql - 在 mysql 表中重新创建编码混合
- javascript - ScrollReveal 'no elements found'?
- javascript - 给定关键字和要搜索的字段列表时如何过滤数组?
- c# - 所有结构的结构扩展方法
- javascript - Meteor:如果我有一个使用 userId 的方法,我是否必须检查他们是否已登录?
- javascript - 运行电子应用程序后如何执行停止脚本?
- javascript - Can i just send the http request I want? Every library seems to screw up something about this
- javascript - 内部 CSS 不适用于 Java 脚本