I am trying to clone the following next.js react app and run it on localhost:3000
when I open localhost:3000 I get the following error
Unhandled Rejection (ReferenceError): fetch is not defined Function._callee$ ./pages/index.js:22
20 | // see
>22 | Index.getInitialProps = async function() {
24 | const url = '';
I have tried to research what I am doing wrong but I still can not fix my problem. Can anyone see where I am going wrong?
Cheers
I am trying to clone the following next.js react app and run it on localhost:3000
https://github./elee-ittdublin/lab6-nextjs
when I open localhost:3000 I get the following error
Unhandled Rejection (ReferenceError): fetch is not defined Function._callee$ ./pages/index.js:22
20 | // see https://nextjs/learn/basics/fetching-data-for-pages
>22 | Index.getInitialProps = async function() {
24 | const url = 'https://api.tvmaze./search/shows?q=batman';
I have tried to research what I am doing wrong but I still can not fix my problem. Can anyone see where I am going wrong?
Cheers
Share Improve this question asked Nov 25, 2018 at 14:39 Craig P HCraig P H 1413 silver badges16 bronze badges 3- what about that answer : stackoverflow./questions/36484156/es6-fetch-is-undefined – Kevin Amiranoff Commented Nov 25, 2018 at 14:57
- thanks for the reply buddy. errors are gone although when i click on the batman links i get '404 This page could not be found.' so the fetch is not working, any idea why? – Craig P H Commented Nov 25, 2018 at 15:28
-
Fetch is working. The issue you have, when you click on the batman links you are redirected to a page on your app that doesn't exist. That line is wrong
<Link as={`/p/${show.id}`} href={`/post?id=${show.id}`}>
– Kevin Amiranoff Commented Nov 25, 2018 at 15:43
3 Answers
Reset to default 2If you go to the examples here examples and search for fetch
, you will see what they are using. In the examples is node-fetch
- node-fetch
fetch
is a predefined method in the browser but your function works in server side, so it does not work.
There is an npm package that basically bines browser's fetch and node-fetch together, it is named isomorphic-unfetch, you can install it via the mand npm install isomorphic-unfetch
.
After that use unfetch
method anywhere u want to use fetch
method.
PS: In the newer versions of Next.JS ( 10.0.0 and above ) you should not see this problem.
May be the package is not installed. Try installing https://www.npmjs./package/isomorphic-unfetch