Javascript 加入firestore(web)中的集合和子集合
我在尝试学习Firestore时遇到了一些关于连接集合和子集合中的数据的问题。子集合的名称是已知的Javascript 加入firestore(web)中的集合和子集合,javascript,vue.js,google-cloud-firestore,Javascript,Vue.js,Google Cloud Firestore,我在尝试学习Firestore时遇到了一些关于连接集合和子集合中的数据的问题。子集合的名称是已知的players -match_stats (collection) |_ document with random ID |_ map (field - string) |_ scorehome (field - Number) |_ scoreaway (field - Number) |_ Sub-collection (named: Players)
players
-match_stats (collection)
|_ document with random ID
|_ map (field - string)
|_ scorehome (field - Number)
|_ scoreaway (field - Number)
|_ Sub-collection (named: Players)
|_ documents with random ids
|_ Field (Player names, ex. Christopher)
|_ playerkills
|_ playerdeaths
我已经能够独立地console.log
每个集合,但是我希望集合和子集合包含在同一个对象中,这样我就可以组合数据了。对于如何解决这个问题,我也愿意接受其他建议
firebase.initializeApp(firebaseConfig);
const db=firebase.firestore();
导出默认值{
数据(){
返回{
matchInfo:[],
};
},
方法:{
readMatches(){
this.matchInfo=[];
数据库集合(“匹配统计数据”)
.get()
.然后(matchQuerysnapshot=>{
matchQuerysnapshot.docs.forEach(doc=>{
console.table(doc.data());
db.collection(“match_stats”).doc(doc.id).collection(“players”)
.get()
.然后(playerQuerySnapshot=>{
playerQuerySnapshot.docs.forEach(doc=>{
console.table(doc.data());
})
})
})
})
},
},
安装的(){
这个.readMatches();
},
};代码>Fire store有自己的钩子,用于在页面加载时抓取数据
data() {
return {
matchInfo: [],
};
},
firestore() {
return {
matchInfo: db.collection('players')
... The rest of your code ...
}
},
你可以这样做。首先获取所有父文档,同时使用Promise.all()并行查询所有players
子集合
var db = firebase.firestore();
var promises = []
db.collection('match_stats').get()
.then(snapshot => {
snapshot.forEach(doc => {
...
promises.push(doc.ref.collection('players').get());
})
return Promise.all(promises);
})
.then(results => {
results.forEach(querySnapshot => {
querySnapshot.forEach(function (doc) {
console.log(doc.id, " => ", doc.data());
});
});
});
请注意,结果
数组的顺序与承诺
数组的顺序完全相同
我的回答是基于这一点做出改变以适应你的问题