我有多个表要基于主表的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": ""
}
}
]
最佳答案
这就是我要做的以获取我想要的答案的方法..只需按一次,然后根据主要子ID调用表..这就是它的样子
promises.push(new Promise((resolve, reject) => {
myhospital.then(hospitalResult => {
myclient.then(clientResult => {
mypatient.then(patientResult => {
resolve({
childKey: childKey,
bookhistory: fk,
hospital: hospitalResult,
client: clientResult,
patient: patientResult
});
});
});
}).catch(reason => {
reject(reason);
});
}));
关于javascript - 如何在node js的firebase中使多个表联接,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48297210/