0

I am working on a react js project and needs to fetch the data from the API.

I am using the fetch() to get the data. Here, is my code.

fetch('https://sandboxapi.ampcome.com/api/gokhana/restaurant/categorized', {
  method: 'GET',
  headers: {
    'Authorization': 'WqQg2jqLWVIO5JBmQ1LYHU8w41FdzLEsCB6yP1Tw2AFHf2w5KWRkMBV8KAgY6Kl5'
  },
  qs: {
    geopoint: {
      near: {
        lat: 12.9699,
        lng: 77.6499
      },
      maxDistance: 50,
      unit: 'kilometers'
    },
    categories: ['trending', 'newaddition', 'popular'],
    outlettypes: ['restaurant', 'foodcourt'],
    clientapp: 'gokhanacustomer'
  }
})
  .then(res => res.json())
  .then(res => console.log(res));

Everything is fine, but this return the empty array. There is a lot of data but it returns an empty array.

I tried the same with axios and it fetches data, I am unable to figure out the exact issue in fetch()

I need to compare the working of axios and fetch that is why I need to test it by both the ways.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Vishal Shetty
  • 1,618
  • 1
  • 27
  • 40
  • Why do you need fetch at all? See,f or example: https://medium.com/@shahata/why-i-wont-be-using-fetch-api-in-my-apps-6900e6c6fe78 – Yossi Jun 14 '18 at 14:06
  • "fetch package" do you mean native `fetch` or which package exactly is your question about? – Yury Tarabanko Jun 14 '18 at 14:11
  • sorry it's native fetch – Vishal Shetty Jun 14 '18 at 14:36
  • You are using `qs` property to pass query string params. Read this question plz [how to pass query string params](https://stackoverflow.com/questions/35038857/setting-query-string-using-fetch-get-request) – Yury Tarabanko Jun 14 '18 at 14:43
  • @YuryTarabanko that solution actually worked fine for me, but the new problem arose, using both `axios` and `fetch` I am getting incomplete data from the API. https://stackoverflow.com/questions/50848651/api-fetches-incomplete-data-from-the-server-in-react-js-project – Vishal Shetty Jun 14 '18 at 16:05

0 Answers0