javascript - 如何使用 Firebase 合并多个集合?

标签 javascript database firebase object

我需要显示 3 个集合中的数据,然后按日期对它们进行排序。问题是我无法找到正确的数据结构来进行设置。

我必须像这样返回一个对象:

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

这是我要设置的代码:

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
        }   

不知何故对象没有正确合并......有什么想法吗?

最佳答案

目前还不清楚您要在这里实现什么,但是合并多个查询的通用方法看起来像这样:

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

关于javascript - 如何使用 Firebase 合并多个集合?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54482517/

相关文章:

javascript - 计数器脚本不倒计时

javascript - AJAX 内容加载后回调运行两次

c# - 如何在访问多个不同的数据库时获得最佳性能

firebase - Firebase控制台中的Google帐户切换

javascript - 如何在create-react-app中导入自制模块?

javascript - 使用 CSS 定位 .slick-current 幻灯片两侧的第一个元素

database - 处理变压器堆栈中的数据库访问

sql - 在数据库中清晰地表示电子商务产品和变体

firebase - 是否可以在一个项目中创建多个 Firebase 实时数据库实例(>20)

ios - 如何在 Firebase 中查询嵌套元素?