如何联接两个表,并在firebase中获取所有基于子级的数据库 [英] how to join two tables and get all data base on child in firebase

查看:74
本文介绍了如何联接两个表,并在firebase中获取所有基于子级的数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从Firebase数据库中的两个表中获取数据 第一张桌子是

i wanted to get the data from two tables in firebase db the 1st table was

我想从这里基于hospital_fk从表中获取下一个数据

from here i want to get the next data from table based on the hospital_fk

这是它在我的json上得到的结果

this is the result it got on my json

这是我获取数据的脚本.

and here is my script for getting the data..

router.get('', function(req, res){
    var booths = database.ref('booths');
    var hospital = database.ref('hospitals');

        booths.once('value', function (snapshot) {
            var dataSet = [];
            snapshot.forEach(function (childSnapshot) {
                    
                var childKey = childSnapshot.key;
                var fk = snapshot.child(childKey).val();
                
                hospital.child(childSnapshot.val().hospital_fk).on('value', hospital=>{
                var childData =  _.assign(fk, hospital.val());
                
                    dataSet.push({
                        childKey: childKey, 
                        childData: childData
                    });

                    res.json(dataSet);
                });
            });

            
        });
});

现在我的问题是仅返回了第一个数据,并且还出现了一个错误..说 FIREBASE警告:用户回调引发了异常.错误:发送标头后无法设置.

now my problem was only the first data is being returned and also getting an error.. says that FIREBASE WARNING: Exception was thrown by user callback. Error: Can't set headers after they are sent.

关于如何获取所有记录以及连接两个表的最佳方法的任何想法.

any idea on how to get all the records and what's the best approach on joining two tables.

推荐答案

调用res.json(dataSet)时,它会设置一个标头,表明您的响应是JSON,然后发送JSON.您只能在发送数据前 的响应上设置标头,因此第二次进行此调用时,Node.js会正确地抛出一个错误,指出无法设置标头.

When you call res.json(dataSet) it sets a header that your response is JSON, and sends the JSON. You can only set headers on the response before sending data, so the second time you make this call, Node.js will rightfully throw an error saying that it can't set the header.

您需要做的是首先将所有加入的数据收集到一个JSON响应中,然后在加载所有数据后一次性发送.为此,您使用一个承诺列表和Promise.all():

What you'll need to do is first gather all joined data into a single JSON response, and then send it in one go after you've loaded all of them. To do this you use a list of promises and Promise.all():

router.get('', function(req, res){
    var booths = database.ref('booths');
    var hospital = database.ref('hospitals');

    booths.once('value', function (snapshot) {
        var promises = [];
        snapshot.forEach(function (childSnapshot) {

            var childKey = childSnapshot.key;
            var fk = snapshot.child(childKey).val();

            var promise = hospital.child(childSnapshot.val().hospital_fk).once('value');

            promises.push(promise);
        });
        Promise.all(promises).then(function(snapshots) {
            var dataSet = [];
            snapshots.forEach(function(hospital) {

                var childData =  _.assign(hospital.key, hospital.val());

                dataSet.push({
                    childKey: hospital.key, 
                    childData: childData
                });

            });
            res.json(dataSet);
        });

    });
});

现在,在获取所有医院数据之后,该代码仅调用一次res.json(dataSet).

Now the code only calls res.json(dataSet) once, after it's gotten all of the hospital data.

您会注意到,我也将您的on('value'更改为once('value',因为我怀疑您想让监听器保持激活状态,而不仅仅是读取一次.

You'll note that I also changed your on('value' to once('value', since I doubt you'll want to keep the listener active for more than just one read.

这篇关于如何联接两个表,并在firebase中获取所有基于子级的数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆