I'm having a function in my server side which returns the results from database. Here's the function
const sql =
`SELECT addresses
FROM Addresses
WHERE JSON_EXTRACT(addresses, '$.address') LIKE ? `
const values = ['"'+beginning+'%']
const query = connection.query(sql, values, (error, results, fields) => {
connection.end()
if (error) {
return reject(error)
}
resolve(results)
})
})
.then((results) => {
return {
type: 'address',
attributes: results
}
})
It's output is like this
{
"data": {
"type": "address",
"attributes": [
{
"addresses": "{\"address\": \"5 street, auburn, nsw 2144\", \"display\": \"5-7 Mary Street, AUBURN, NSW 2144\"}"
},
{
"addresses": "{\"address\": \"5 street, lidcombe, nsw 2141\", \"display\": \"5 Street, LIDCOMBE, NSW 2141\"}"
}
]
}
}
I'm new to JS and I need to make the above output as follows
{
"data": [
{
"type": "address",
"id": "5-13 aubrey street, granville, nsw 2142",
"attributes": {
"address": "5-13 aubrey street, granville, nsw 2142",
"display": "5-13 Aubrey Street, GRANVILLE, NSW 2142"
}
},
{
"type": "address",
"id": "5 street, lidcombe, nsw 2141",
"attributes": {
"address": "5 street, lidcombe, nsw 2141",
"display": "5 Street, LIDCOMBE, NSW 2141"
}
},
{
"type": "address",
"id": "5-7 mary street, auburn, nsw 2144",
"attributes": {
"address": "5-7 mary street, auburn, nsw 2144",
"display": "5-7 Mary Street, AUBURN, NSW 2144"
}
}
}
]
}
Can someone help me to change .then((results) body of my function to make this happen.
In the client side I'm getting the results inside a function and this is how I return it
return $http.get(`${API_URL}/addresses/?beginning=${searchTerm || ''}`)
.then((result) => {
// Only want to return the address string
return result.data.data((address) => address.attributes.display)
})
But I'm getting result.data.data is not a function