0

I'm using fetch() for the first time to get some data from the API: 'https://www.codewars.com/api/v1/users/thenarfer'.

My goal is to console.log the data. I believe the data to be in JSON format.

The error I get with the following code is:

Uncaught ReferenceError: data is not defined

Is POST or GET the correct approach? How do I continue from here?

fetch('https://www.codewars.com/api/v1/users/thenarfer', {
    method: 'GET',
    mode: 'no-cors',
    credentials: 'include',
    body: JSON.stringify(data),
    headers: {
      'Content-Type': 'application/json'
    }
  }).then((res) => res.json())
  .then((data) => {
    console.log(data)
  })
  .catch((err) => console.log(err));
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
thenarfer
  • 405
  • 2
  • 14
  • can this be because of data you are passing in body? – Shyam May 10 '21 at 12:28
  • 1
    Pretty sure `data` in stringify is not defined. – OddDev May 10 '21 at 12:28
  • It seems to work fine without all the options `fetch('https://www.codewars.com/api/v1/users/thenarfer').then(...` – Reyno May 10 '21 at 12:30
  • The duplicate isn't actually a duplicate of _this_ problem. As OddDev points out, you haven't defined `data`. (The use of multiple variables named `data` was confusing). The duplicate does cover the next problem you'll have though. – Quentin May 10 '21 at 12:31
  • Thanks a lot! As you pointed out there are at least two problems. 'data' and 'no-cors'. I will try to resolve both of them and post a new question with the progress. @Quentin – thenarfer May 10 '21 at 12:36
  • @Reyno You solved it all for me! Thank you! You should have heard the "Wow" that came out of me when I tried this! – thenarfer May 10 '21 at 12:41

0 Answers0