javascript - Hur slår man samman flera samlingar med Firebase?


Jag behöver visa data från 3 samlingar och sedan sortera dem efter datum. Problemet är att jag inte kan hitta rätt datastruktur för att ställa upp detta.

Jag måste returnera ett objekt som detta :

data : {
    total,   // the total number of objects in collections
    data     // the data from all the three collections
}

Här är koden som jag försöker ställa in:

const fillAray = (callb) => {
            let total = 0;
            let totalData={}
            for(let i=0;i<3;i++){
                switch(i){
                    case 0:
                    historyPart='1st table'
                    break;
                    case 1:
                    historyPart='2nd table'
                    break;
                    case 2:
                    historyPart='3rd table'
                    break;
                }
                
                const collection_ref = admin.firestore().collection(`*****-${historyPart}`);
                const user_transactions_ref = collection_ref.doc(uid).collection('transactions');
                user_transactions_ref.orderBy('time', 'desc').get().then(
                    (snapshot) => {
                        if (!snapshot.empty) {
                            snapshot.forEach((doc) => {
                                ++total;
                            });
                        }
                        user_transactions_ref.orderBy('time', 'desc').offset(from).limit(size).get().then(
                            (snapshot) => {
                                const out = [];
                                if (!snapshot.empty) {
                                    snapshot.forEach((doc) => {
                                        out.push(doc.data())
                                    });
                                }
                                //merging data
                                Object.assign(totalData,out)
                                
                                
                            },
                            (error) => { callback(error, null); }
                        );
                    },
                    (error) => { callback(error, null); }
                );
                
            }
             sendDataToFront(totalData,total)
            // Here I'm supposed to have totalData and total with all datas
        }   

På något sätt smälter inte objektet ihop ordentligt ... Några idéer?

Svar #1

Det är oklart exakt vad du försöker uppnå här, men en generell metod för att slå ihop flera förfrågningar skulle se ut ungefär så här:

const refA = admin.firestore().collection('foo/A/widgets');
const refB = admin.firestore().collection('foo/B/widgets');

// fetch data in parallel
Promise.all([ refA.get(), refB.get() ])

  // merge the results
  .then(promiseResults => {
     const mergedData = [];
     promiseResults.forEach( snapshot => {
        snapshot.forEach( doc => mergedData.push(doc.data()) );
     });
     return mergedData;
  })

  // sort the results
  .then(mergedData => mergedData.sort((a, b) => a.timestamp - b.timestamp))

  // use the results
  .then(sortedData => {
     // do something with data here
     console.log(sortedData);
  })

  // log any errors
  .catch(e => console.error(e));