I really want to make primary array have the property of foreign key
I have 2 table the tipe_akademik and jam_akademik_a
Then the jam_akademik have foreign key to tipe_akademik
The next I want show as tipe_akademik have list of jam_akademik_a being called list_mapel
I want make the array like :
{
"data": [
{
"id": 1,
"tipe_akademik": "a",
"listmapel": [{//from jam_akademik_a//
"id": 1,
"mapel": "Pendidikan Agama Islam",
"tipe_aka": 1
},
{
"id": 10,
"mapel": "Bahasa Indo",
"tipe_aka": 1
}
]
}, and etc
In top of my array the first id is primary key from tipe_akademik while tipe _aka is foreign key to that id , then I try to make 1 array from 2 query
My source code
router.get('/test', function (req ,res) {
//query
let tipe= []
connection.query(`SELECT * FROM tipe_akademik `, function (err, result){
tipe=result
let i = 0
for (let mapeltipe of tipe ){connection.query ('SELECT * FROM jam_akademik_a WHERE tipe_aka = ? ' ,[mapeltipe.id] ,function (err, listmapel){
tipe[i].listmapel = listmapel
i++
}
)}return res.status(500).json ({data: tipe})
}
)
});
But when I return it the array tipe still showing the first query only. The array I doing for looping is not saved in the array tipe. Thanks if someone can help me... for making that 2 query in one array with the looping.