Code Monkey home page Code Monkey logo

Comments (8)

hnordt avatar hnordt commented on April 28, 2024 6

Yeah, same problem here @clintonhalpin, I think Node.js doesn't support natively fetch yet. A work around would be to install and import the package isomorphic-fetch.

So the example could be changed to something like:

import React from 'react'
import 'isomorphic-fetch'
export default class extends React.Component {
  static async getInitialProps () {
    const res = await fetch('https://api.company.com/user/123')
    const data = await res.json()
    return { username: data.profile.username }
  }
}

from next.js.

clintonhalpin avatar clintonhalpin commented on April 28, 2024 1

@hnordt Nice, much cleaner!

from next.js.

nodegin avatar nodegin commented on April 28, 2024 1

next.js should absolutely include the isomorphic-fetch polyfill!

from next.js.

rauchg avatar rauchg commented on April 28, 2024 1

I was missing the isomorphic-fetch import. Thank you. We actually use that extensively for www.zeit.co

from next.js.

rauchg avatar rauchg commented on April 28, 2024 1

We'll update the blog post

from next.js.

hnordt avatar hnordt commented on April 28, 2024 1

Anyway, great job with Next.js, you and Zeit's team are brillant @rauchg.

from next.js.

clintonhalpin avatar clintonhalpin commented on April 28, 2024

+1

Also I'm not sure if I'm doing something wrong. But when I ran the example ( with correction ) Node v.7.0 was complaining that fetch wasn't defined.

My solution was to import axios and use that instead of fetch. I'd be happy to open a PR for this in the examples folder!

import React from 'react'
import axios from 'axios'

export default class extends React.Component {

  static async getInitialProps () {
    const response = await axios('https://api.company.com/user/123')
    return { username: response.data }
  }

}

from next.js.

hnordt avatar hnordt commented on April 28, 2024

The blog was updated, I'm closing this issue.

from next.js.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.