0

getting started learning React. Trying to fetch list of users from jsonplaceholder. throwing error what .json() undefined metod and fetch() is undefined too. sound like rubbish. As i understand node.js dont have fetch() and i must downloading lib but that lib dont know what is .json()

const gettingData = async () => {
    const response = await fetch('https://jsonplaceholder.typicode.com/users/1');
    const data = await response.json();
    console.log(data)
};
gettingData()
    .catch((err) => {
    console.log(err)
});

this code throwing

ReferenceError: fetch is not defined
    at gettingData (C:\Dev\chartsandtable\src\components\Pie\PieData.js:5:22)
    at Object.<anonymous> (C:\Dev\chartsandtable\src\components\Pie\PieData.js:9:1)
    at Module._compile (internal/modules/cjs/loader.js:959:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:995:10)
    at Module.load (internal/modules/cjs/loader.js:815:32)
    at Function.Module._load (internal/modules/cjs/loader.js:727:14)
    at Function.Module.runMain (internal/modules/cjs/loader.js:1047:10)
    at internal/main/run_main_module.js:17:11

so i find familiar question install node-fetch and get another one err - .json() not a method. How fix it? Or switch on axios?

d2207
  • 79
  • 1
  • 8
  • 3
    The current error message is indeed solved by the question linked. Please update the code with this change and expand the other error message. – E_net4 Jun 08 '20 at 11:40
  • Your error may be due to you are trying to invoke your gettingData at the wrong time. Unless you use server side rendering you shouldn't invoke that function during node process. – Arseniy-II Jun 08 '20 at 14:19
  • All thx! I just paste code from tutorial and its working. – d2207 Jun 09 '20 at 09:11

0 Answers0