【问题标题】:fetching multiple data using firebase/firestore on the server side (nextjs/apiRoutes) =problem => output before the data is ready在服务器端(nextjs/apiRoutes)使用firebase/firestore获取多个数据=问题=>在数据准备好之前输出
【发布时间】:2020-05-24 10:20:47
【问题描述】:

result[] 将在数据准备好之前执行。我什至试图用承诺来解决问题,但也没有成功..

import { firestore } from "../../../firebase";

export default (req, res) => {
  firestore
    .collection("category-filters")
    .where(req.query.categoryKey, "==", true)
    .get()
    .then(querySnapshot => {
      let result = [];

      querySnapshot.docs.map(doc => {
        firestore
          .collection("payment-accounts")
          .doc(doc.id)
          .get()
          .then(item => {
            if (item.data().creditAmount > 0) {
              firestore
                .collection("professional-profiles")
                .doc(item.id)
                .get()
                .then(endresult => {
                  result.push({ id: endresult.id, ...endresult.data() }); // executes successful
                });
            }
          });
      });

      res.json({ result }); // the data is outputted before the "querySnapshot.docs.map" is executed...
      // therefore received a blank array (result)
    });
};

【问题讨论】:

    标签: reactjs firebase google-cloud-firestore server-side next.js


    【解决方案1】:

    以下应该可以工作(未经测试):

    export default (req, res) => {
        firestore
            .collection("category-filters")
            .where(req.query.categoryKey, "==", true)
            .get()
            .then(querySnapshot => {
    
                const promises = [];
                const paymentAccountsRef = firestore.collection("payment-accounts");
    
                querySnapshot.docs.map(doc => {
                    promises.push(paymentAccountsRef.doc(doc.id).get());
                });
    
                return Promise.all(promises);
    
            })
            .then(paymentAccountsSnapshotsArray => {
    
                const promises = [];
                const professionalProfilesRef = firestore.collection("professional-profiles");
    
                paymentAccountsSnapshotsArray.forEach(snap => {
                    if (snap.data().creditAmount > 0) {
                        promises.push(professionalProfilesRef.doc(snap.id).get());
                    }
                });
    
                return Promise.all(promises);
    
            })
            .then(professionalProfileSnapshotsArray => {
    
                const results = [];
    
                professionalProfileSnapshotsArray.forEach(snap => {
                    results.push({ id: snap.id, ...snap.data() });
                });
    
                res.json({ results });
    
            })
    };
    

    您需要使用Promise.all() 管理并行文档获取,并将Promise.all() 返回的不同Promise 链接起来,以便仅在所有这些异步操作完成后才发回响应。在您当前的代码中,您将在这些异步操作完成之前发回响应。


    此外,您可能需要微调此代码,以检查不同的快照数组是否为空。这部分由您决定!

    【讨论】:

    • 亲爱的 Tarnec,非常感谢!您的代码运行无错误!
    猜你喜欢
    • 1970-01-01
    • 2021-10-28
    • 2022-11-21
    • 2015-10-12
    • 2017-10-14
    • 1970-01-01
    • 1970-01-01
    • 2019-10-22
    • 1970-01-01
    相关资源
    最近更新 更多