如何在节点js中的firebase中进行多表连接

时间:2018-01-17 09:03:54

标签: javascript node.js firebase firebase-realtime-database

我有多个表,我想根据主表的id加入。 这是我的剧本。

var bookinghistory = database.ref('bookingHistory');
var hospital = database.ref('hospitals');
var client = database.ref('clients');
var patient = database.ref('patients');

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

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

            var myhospital = hospital.child(childSnapshot.val().hospital_fk).once('value');
            var myclient = client.child(childSnapshot.val().client_fk).once('value');
            var mypatient = patient.child(childSnapshot.val().patient_id).once('value');

            promises.push(new Promise((resolve, reject) => {
                 myhospital.then(docResult => {
                    resolve({         
                        childKey: childKey,
                        bookhistory: fk,
                        hospital: docResult
                    });
                }).catch(reason => {
                    reject(reason);
                 });
             }));
            promises.push(new Promise((resolve, reject) => {
                 myclient.then(docResult => {
                    resolve({         
                        client: docResult
                    });
                }).catch(reason => {
                    reject(reason);
                 });
             }));
            promises.push(new Promise((resolve, reject) => {
                 mypatient.then(docResult => {
                    resolve({         
                        patient: docResult
                    });
                }).catch(reason => {
                    reject(reason);
                 });
             }));
       }));
});
Promise.all(promises).then(function(snapshots) {
        var dataSet = [];
         snapshots.forEach(function(hospital) {

            dataSet.push({
                childKey: hospital.childKey, 
                booth: hospital.booth,
                hospital:hospital.hospital,
                client:hospital.client,
                patient:hospital.patient

            });

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

这是我的脚本的结果。

[
    {
        "childKey": "-L2snm8lhye",
        "hospital": {
            "address": "Rizal Drive cor. ",
            "city": "",
            "createdAt": "2018-01-14 23:01:32",
            "deletedAt": "",
            "latitude": "14.554928",
            "longitude": "1115",
            "name": "St. Luke's Medical Center - Global City",
            "province": "",
            "updatedAt": ""
        }
    },
    {
        "client": {
            "createdAt": "2018-01-15",
            "firstName": "",
            "identifier": "+63933000000",
            "lastName": ""
        }
    },
    {
        "patient": {
            "birthdate": "1993-01-15",
            "client_fk": "qFUJT05B19NgQ2",
            "createdAt": "2018-01-15 15:02:13",
            "deletedAt": "",
            "firstName": "Eric Christian",
            "gender": "1",
            "lastName": "Odulio",
            "updatedAt": ""
        }
    }
]

我想要的是客户端,患者数据显示在医院阵列之后......它发生了什么事情在外面..这是我想要退回的json ..有什么办法可以做到这一点吗?

[
    {
        "childKey": "-L2snm8lhye",
        "hospital": {
            "address": "Rizal Drive cor. ",
            "city": "",
            "createdAt": "2018-01-14 23:01:32",
            "deletedAt": "",
            "latitude": "14.554928",
            "longitude": "1115",
            "name": "St. Luke's Medical Center - Global City",
            "province": "",
            "updatedAt": ""
        }
        "client": {
            "createdAt": "2018-01-15",
            "firstName": "",
            "identifier": "+63933000000",
            "lastName": ""
        }
        "patient": {
            "birthdate": "1993-01-15",
            "client_fk": "qFUJT05B19NgQ2",
            "createdAt": "2018-01-15 15:02:13",
            "deletedAt": "",
            "firstName": "Eric",
            "gender": "1",
            "lastName": "Odulio",
            "updatedAt": ""
        }
   }
]

1 个答案:

答案 0 :(得分:0)

这就是我所做的,以获得我想要的答案..只需按一次然后根据主要子ID调用表..所以这就是它的样子

void Handle_Chat_Tapped(object sender, System.EventArgs e)
{
    var menuItem = sender as MenuItem;
    // whatever you want to do here
}