简体   繁体   English

如何从 Firestore 两个 collections 获取数据并将所有数据合并在一起

[英]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.并将数据添加到获胜者列表数组中,但它不能按我的意愿工作。

  1. winnerlist order is not correct获奖名单顺序不正确
  2. return res.send({ winnerList: result });返回 res.send({ 获胜者列表:结果 }); part also not working部分也不起作用

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.

相关问题 如何从Firestore中的两个集合中获取数据? - How do I get data from two collections in firestore? Firestore join foreach map - 从两个 collections 获取数据 - Firestore join foreach map - get data from two collections 如何在本机反应中显示来自两个 Firestore collections 的数据 - How to display data from two Firestore collections in react native 如何从 firebase firestore 异步获取所有父 collections 的所有文档? - How can I get all the documents of a all parent collections asynchronously from firebase firestore? 在 react-native 中运行之前,如何让应用程序等待从从 firestore 获取数据的模块中获取所有数据? - How can i make app wait to get all data from module that get data from firestore before run in react-native? 我如何从 Firestore 文档中获取所有数据? - How would I get all data from firestore document? 试图从 Firestore 获取所有收集数据 - Trying to get all the collection data from Firestore 如何在MongoDB中联接来自两个集合的数据? - How to join data from two collections in MongoDB? 如何根据文档中的参考ID合并并观察Firestore中的两个集合? - How to merge and observe two collections in Firestore based on reference ID in documents? Firebase Firestore - 比较来自 2 collections 的数据 - Firebase Firestore - Compare data from 2 collections
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM