Next.js: fetch is not defined

Created on 16 Apr 2017  路  1Comment  路  Source: vercel/next.js

I tried the second example of fetching, and I get this error (nextjs version: 2.1.1):

const Page = ({ stars }) => <div>Next stars: {stars}</div>

Page.getInitialProps = async ({ req }) => {
  const res = await fetch('https://api.github.com/repos/zeit/next.js')
  const json = await res.json()
  return { stars: json.stargazers_count }
}

export default Page

screen shot 2017-04-16 at 18 47 56

Most helpful comment

You may need to import a package like this: https://www.npmjs.com/package/isomorphic-unfetch which works both on the server and client.

Follow this tutorial: https://learnnextjs.com/basics/fetching-data-for-pages

>All comments

You may need to import a package like this: https://www.npmjs.com/package/isomorphic-unfetch which works both on the server and client.

Follow this tutorial: https://learnnextjs.com/basics/fetching-data-for-pages

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ghost picture ghost  路  3Comments

wagerfield picture wagerfield  路  3Comments

YarivGilad picture YarivGilad  路  3Comments

kenji4569 picture kenji4569  路  3Comments

sospedra picture sospedra  路  3Comments