Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Api Platform - Hydra - accessing hydra:member with javascript

Tags:

javascript

I am having problem that I cannot solve. I need to print out response objects with javascript and I don't know how to do that. I have response like.this:

{
  "@context": "/contexts/Client",
  "@id": "/clients",
  "@type": "hydra:Collection",
  "hydra:member": [
    {
      "@id": "/clients/1",
      "@type": "Client",
      "uuid": "e3rer445",
      "number": " 0483",
      "name": "Tom Beringer",
      "adresses": [
      {
          "@id": "/client_addresses/1",
          "@type": "http://schema.org/Thing",
          "address": {
              "@id": "/addresses/1",
              "@type": "http://schema.org/Thing",
              "address": "Postbus 1d425"
          }
      },
  ]
},

And now I need to print out the result of all client details, so when I do this:

axios.get('/clients')
      .then(res => {
      this.users = res.data['hydra:member']
  })

I successfully print out the name, number, but when I try to print out addresses i get.this as a result:

<td><span>[
  {
    "@id": "/client_addresses/3",
    "@type": "http://schema.org/Thing",
        "address": {
        "@id": "/addresses/3",
        "@type": "http://schema.org/Thing",
        "address": "niet meer gebruiken"
     }
  } 
</span></td>

But what I need is just.this address: niet meer gebruiken Is it possible to do that?


1 Answers

Yes, It is possible.

Try with this:

res.data['hydra:member'][0]['adresses'][0]['address']['address']

or

res.data['hydra:member'][0]['adresses'][0].address.address

That will work fine if you only have one address object nested. In case you have more objects inside you should iterate over the array of addresses.

This will be/contain your array of addresses:

 res.data['hydra:member'][0]['adresses']
like image 193
Manuel E Carpio Avatar answered Sep 13 '25 07:09

Manuel E Carpio