Search code examples
node.jsaxiosxmlhttprequestodata

how to access odata from http request


Hello I'm using axios for my get request and I am able to get a response from the request, however I want to get access to odata within the object that is returned.

here is the returned object from res.data

{
  '@odata.context': 'https://ddf.house.com/odata/v1/$metadata#Property(ListingKey)',
  value: [
    {
      ListingKey: '20586562',
      
    },
    {
      ListingKey: '22792237',
     
    }
  ],
  '@odata.nextLink': "https://ddf.house.ca/odata/v1/Property?$top=2&$skip=2&$select=ListingKey"
}

I'm trying to access the @odata.nextLink property.

So to access the value I just use

.then((res) => {
            console.log(res.data.value);
        })

But how do I access odata.nextLink? using the dot notation won't allow me to use something like this.

res.data.@odata.nextLink

Solution

  • You can access an array item by [] operator

    Using this code

    res.data["@odata.context"]
    

    Demo - running local own REST API server, call it by axios then access @odata.nextLink

    const axios = require("axios");
    
    axios.get("http://localhost:3001/users")
        .then(res => {
            console.log(JSON.stringify(res.data, null, 4))
            console.log('-------------------------------------')
            console.log(res.data["@odata.context"])
        })
    

    result

    {
        "@odata.context": "https://ddf.house.com/odata/v1/$metadata#Property(ListingKey)",
        "value": [
            {
                "ListingKey": "20586562"
            },
            {
                "ListingKey": "22792237"
            }
        ],
        "@odata.nextLink": "https://ddf.house.ca/odata/v1/Property?$top=2&$skip=2&$select=ListingKey"
    }
    -------------------------------------
    https://ddf.house.com/odata/v1/$metadata#Property(ListingKey)
    

    enter image description here