簡體   English   中英

Firestore join foreach map - 從兩個 collections 獲取數據

[英]Firestore join foreach map - get data from two collections

嘿伙計們,我還需要你的幫助:( 我不知道我是如何在 Firebase 中加入或做其他事情的

我有兩個 collections:collection("guests") 在第一步中有一個數據字段“Eventid”和一個數據字段“Userid”我 select 所有客人都具有特定的 Userid。 所以我在 foreach 循環(或作為數組)中獲取所有 Eventid。 在第二個 Collection collection('events') 中,我將所有信息 select 寫入 JSON 文件中。 幾次后這也有效,這是我的問題。 我在功能中運行它,在加載事件之前返回 function。 我不知道我是如何使用它的,我用 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);
        });
    });
});

   

我知道了!

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
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM