Using a microservice, I fetch multiple sets of JSON data and display them on a Vue.js-powered page. The structure of the data is as follows:
{"data":{"getcompanies":
[
{"id":6,"name":"Arena","address":"12 Baker Street","zip":"15090"},
{"id":7,"name":"McMillan","address":null,"zip":"15090"},
{"id":8,"name":"Ball","address":"342 Farm Road","zip":"15090"}
]
}}
{"data":{"getusers":
[{"id":22,"name":"Fred","address":"Parmesean Street","zip":"15090"},
{"id":24,"name":"George","address":"Loopy Lane","zip":"15090"},
{"id":25,"name":"Lucy","address":"Farm Road","zip":"15090"}]}}
{"data":{"getdevices":
[{"id":2,"name":"device type 1"},
{"id":4,"name":"device type 2"},
{"id":5,"name":"device type 3"}]}}
I have successfully extracted this data individually using code like this:
getCompanies() {
this.sendMicroServiceRequest({
method: 'GET',
url: `api/authenticated/function/getcompanies`
})
.then((response) => {
if(response.data) {
this.dataCompanies = response.data.getcompanies
} else {
console.error(response)
}
}).catch(console.error)
}
The methods getUsers()
and getDevices()
function in a similar manner. The output of getCompanies()
is:
[{"id":6,"name":"Arena","address":"12 Baker Street","zip":"15090"},
{"id":7,"name":"McMillan","address":null,"zip":"15090"},
{"id":8,"name":"Ball","address":"342 Farm Road","zip":"15090"}]
This data is then displayed in a table on the Vue template without any issues.
However, managing additional microservices calls could become cumbersome in the future.
I am seeking a more efficient way to bypass the response.data.*whatever*
and directly access those id records with a reusable call. Unfortunately, I am facing challenges in achieving this. I have tried various approaches but haven't been successful in accessing the required data chunk smoothly.
Even though my latest attempt does yield results, it presents individual array elements which I would prefer to avoid manipulating into a JSON structure.
If you have any suggestions, hints, or guidance on how to streamline this process, please share your insights. They will be greatly appreciated!