Javascript RN/Firestore-如何使用Firestore引用对象填充嵌套数组
我正在尝试使用Firebase/Firestore获得这样的输出Javascript RN/Firestore-如何使用Firestore引用对象填充嵌套数组,javascript,firebase,react-native,foreach,google-cloud-firestore,Javascript,Firebase,React Native,Foreach,Google Cloud Firestore,我正在尝试使用Firebase/Firestore获得这样的输出 "topRecipes": [ { "id": "LGvuYa0pN2432fd", "created": 1531480386994, "prep": 25, "cook": 2 "ingredients": [ { "ingName": "golden caster sugar", "weight": 300, }, { "ingName": "b
"topRecipes": [
{
"id": "LGvuYa0pN2432fd",
"created": 1531480386994,
"prep": 25,
"cook": 2
"ingredients": [
{
"ingName": "golden caster sugar",
"weight": 300,
},
{
"ingName": "butter",
"weight": 300,
},
{
"ingName": "self-raising flour",
"weight": 300,
},
],
"recipeName": "Butterfly cupcakes",
"username": "John Doe",
"uid": "c1Mi03C7KxOpKceazn8GLovGGW73",
},
]
我使用Firestore引用对象(如外键)来填充内部(配料)数组,但我觉得我尝试的内容非常混乱,甚至无法正常工作
constructor(){
超级()
this.state={selectedIndex:0,topRecipes:[]};
this.recipesInit=[];
}
组件willmount(){
让互惠主义者;
db.collection(“Recipes”).orderBy(“created”、“asc”).get().then((qss)=>{
qss.forEach((文件)=>{
const{created,prep,cook,recipeName,username,uid,flavorListRef}=doc.data();
recipeList={
id:doc.id,
已创建:已创建。时间戳,
准备:准备,
厨师:厨师,
recipeName:recipeName,
用户名:用户名,
uid:uid,
}
if(flavorListRef){
让combineRecipes={…recipeList,flavorList:[]};
flavorListRef.collection(“flavorList”).get()。然后((sqss)=>{
sqss.forEach(文档=>{
const{ingName,weight}=docs.data();
combineRecipes.flavorList.push({
ingName:ingName,
重量:重量,,
})
})//forEach
})
.然后(()=>{
this.recipesInit.push(…this.recipesInit,combineRecipes)
this.setState({topRecipes:this.recipesInit})
})
.catch(err=>console.error(err));
}
})//forEach
})
.catch(err=>console.error(err));
}
看来我所要做的就是
改变这个
this.recipesInit.push(…this.recipesInit,combineRecipes)
到
this.recipesInit.push(combineRecipes)