最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to merge multiple collections with Firebase? - Stack Overflow

programmeradmin3浏览0评论

I need to display data from 3 collections and then sort them by date. The problem is that I can't manage to find the right data structure to set this up.

I have to return an object like this :

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

Here is the code that I'm trying to setup :

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
        }   

Somehow the object don't merge properly ... Any ideas ?

I need to display data from 3 collections and then sort them by date. The problem is that I can't manage to find the right data structure to set this up.

I have to return an object like this :

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

Here is the code that I'm trying to setup :

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
        }   

Somehow the object don't merge properly ... Any ideas ?

Share Improve this question edited Sep 23, 2021 at 17:03 marc_s 756k184 gold badges1.4k silver badges1.5k bronze badges asked Feb 1, 2019 at 15:32 Lucas LareginieLucas Lareginie 771 silver badge9 bronze badges 4
  • Hi Lucas
发布评论

评论列表(0)

  1. 暂无评论