【问题标题】:Firestore join foreach map - get data from two collectionsFirestore join foreach map - 从两个集合中获取数据
【发布时间】:2020-12-24 03:39:51
【问题描述】:

嘿,伙计们,我还需要你的帮助 :( 我不知道如何在 Firebase 中进行加入或其他操作

我有两个系列: collection("guests") 有一个数据字段“Eventid”和一个数据字段“Userid” 在第一步中,我选择具有特定用户 ID 的所有客人。所以我在 foreach 循环(或作为数组)中获取所有 Eventid!在第二个集合 collection('events') 我选择所有信息并将其写入 JSON 文件。几次后这也有效,这是我的问题! 我在函数中运行它,函数在加载事件之前返回。 我不知道如何使用它,我尝试使用 await 和 async 或将其拆分为两个函数。 也许还有另一种实现方式。

db.collection("guests").where("userid", "==", id).get().then(function(querySnapshot) {
    querySnapshot.forEach(function (doc) {
        db.collection('events').doc(doc.data().eventid).get().then(function(doc) {
            if (doc.exists) {
                console.log(doc.id, " => ", doc.data());
            } else {
                console.log("No such document!");
            }
        }).catch(function(error) {
            console.log("Error getting document:", error);
        });
    });
});

   

【问题讨论】:

    标签: javascript firebase google-cloud-firestore foreach google-cloud-functions


    【解决方案1】:

    我明白了!

    exports.getEvent = functions.https.onRequest(async (req, res) => {
        console.log('==NACHEINANDER STARTEN==');
      
        const id = req.query.uid;
        var arr = [];
        var json = [];
        let query =  db.collection("guests").where("userid", "==", id);
        
    
    await query.get().then(querySnapshot => {
            querySnapshot.forEach(documentSnapshot => {
                arr.push(documentSnapshot.data());
            });
        });
        //console.log(arr);
    await processArray(arr)     
    
    
    async function delayedLog(item) {
        await db.collection('events').doc(item.eventid).get().then(function(doc) {
            console.log(doc.data());
            json.push(doc.data());
        })
    
    }
    
    async function processArray(array) {
        const promises = array.map(delayedLog);
        // wait until all promises are resolved
        await Promise.all(promises);
        console.log('Done!');
     }
    
    
    console.log("hello");
    
    
        res.status(200).send(JSON.stringify(json)); //, ...userData
    });
    

    【讨论】:

      猜你喜欢
      • 2019-06-05
      • 2022-11-06
      • 2019-03-12
      • 2018-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多