4

I am trying to clone the following next.js react app and run it on localhost:3000

https://github.com/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.org/learn/basics/fetching-data-for-pages
>22 | Index.getInitialProps = async function() {
24 |   const url = 'https://api.tvmaze.com/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

3
  • what about that answer : stackoverflow.com/questions/36484156/es6-fetch-is-undefined 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? 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}`}> Commented Nov 25, 2018 at 15:43

3 Answers 3

2

If 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

Sign up to request clarification or add additional context in comments.

Comments

1

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 combines browser's fetch and node-fetch together, it is named isomorphic-unfetch, you can install it via the command 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.

Comments

0

May be the package is not installed. Try installing https://www.npmjs.com/package/isomorphic-unfetch

2 Comments

thanks for the reply; i tried installing the package. i'm still getting the same errors
... and this is more like a comment than an answer (especially since it does not seem to address OP's issue).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.