how to join two tables and get all data base on child in firebase

Deadly 提交于 2020-06-23 05:15:31

问题


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

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

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

            
        });
});

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.


回答1:


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.

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

    });
});

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

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.



来源:https://stackoverflow.com/questions/48222310/how-to-join-two-tables-and-get-all-data-base-on-child-in-firebase

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!