I am trying to set the primary array to have the property of a foreign key
I have two tables, tipe_akademik and jam_akademik_a
The jam_akademik table has a foreign key pointing to tipe_akademik
My goal is to display tipe_akademik as having a list of jam_akademik_a, referred to as list_mapel
This is how I want the array to look 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 my array, the first ID is the primary key from tipe_akademik, and tipe_aka is a foreign key pointing to that ID. I am attempting to combine the results of two queries into one array.
Here is a snippet of 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})
}
)
});
However, when I return it, the tipe array still only shows the results of the first query. The looping I attempted to do to combine the two queries is not saving the data in the tipe array. I would appreciate any help in resolving this issue and combining the two queries into one array.