0

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

1 Answer 1

1

You can change the return to be

return {
    type: 'address',
    attributes: results.map(result => JSON.parse(result.addresses))
}
Sign up to request clarification or add additional context in comments.

4 Comments

From the client side after calling the API , I'm trying to get results as return result.data((address) => address.attributes.display) but it's not working. Can you help me to match this to above answer?
@kavish i am not sure what exactly you want to accomplish, can you explain ?
Thank you a lot for replying. I updated the question. please check the last part 'in my client side..
@kavish you have to call map, like return result.data.data.map((address) => address.attributes.display)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.