[英]how can get data from firestore two collections and merge all data together
first I tried to get data from the leaderboards collection and then I tried to get users details首先我尝试从排行榜集合中获取数据,然后我尝试获取用户详细信息
and add data into winnerlist array but it doesn't work as I want.并将数据添加到获胜者列表数组中,但它不能按我的意愿工作。
what is the best way to do this?做这个的最好方式是什么? what I'm doing wrong here?我在这里做错了什么? I'm kinda new to this plz help me what I'm expecting is winnerlist array with all users' details and the correct order.我对此有点陌生,请帮助我期待的是带有所有用户详细信息和正确顺序的获胜者列表数组。
const leaderboardsCollectionRef = db.collection('leaderboards');
const usersCollectionRef = db.collection('users');
const winnerList = [];
const promise1 = new Promise((resolve, reject) => {
leaderboardsCollectionRef.orderBy('weeklyPoints', 'desc').limit(13).get().then(async results => {
results.forEach(async doc => {
return new Promise((resolve, reject) => {
const users = usersCollectionRef.where("uid", "==", doc.id).get().then(async snapshot => {
snapshot.forEach(async doc => {
winnerList.push({
[doc.id]: doc.data()
});
});
resolve(winnerList);
}).catch(err => {
functions.logger.error('ERR =>', err);
reject(err);
});
});
});
}).catch(err => {
functions.logger.error('ERR =>', err);
reject(err);
});
});
Promise.all([promise1])
.then((result) => {
console.log('result', result)
return res.send({
winnerList: result
});
}).catch(err => {
functions.logger.error('ERR =>', err);
});
Assuming you'll wrap this in an async
function, I guess this will work:假设您将其包装在async
function 中,我想这将起作用:
const leaderboardsRef = db.collection('leaderboards');
const usersCollectionRef = db.collection('users');
const winnerList = [];
const resultsQuery = leaderboardsRef.orderBy('weeklyPoints', 'desc').limit(13);
const resultsSnapshot = await resultsQuery.get();
const uids = [];
resultsSnapshot.forEach(doc => {
uids.push(doc.id);
});
for (const uid of uids) {
const userRef = usersCollectionRef.doc(uid);
const userSnapshot = await userRef.get();
winnerList.push({ [uid]: userSnapshot.data() });
}
res.send({ winnerList });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.