I have 2 tables in my SQLLite db
Table 1: Location
| id | name |
|---|---|
| 1 | USA |
| 2 | UK |
Table 2: Components
| id | name | locationId |
|---|---|---|
| 1 | John | 1 |
| 2 | Jack | 1 |
| 3 | Jill | 2 |
| 4 | Sam | 2 |
I have a endpoint like:
app.get('/location/:id', (req, res, next) => {
const component1= db.all(
`Select name FROM Components Where locationId = ${req.params.id}`
).then(comp => res.json(comp))
.catch(next)
}
and it returns:
[
{
"name": "John"
},
{
"name": "Jack"
}
]
BUT the expected result is:
if the endpoint is localhost:8080/location/1 it should return :
{
name: 'USA',
components: [
{
"name": "John"
},
{
"name": "Jack"
}
]
}
I'm using ExpressJS and this endpoint in app.js file