GithubHelp home page GithubHelp logo

Comments (6)

lancemccluskey avatar lancemccluskey commented on June 21, 2024

I am getting this error too. I was able to fix it by adding `typeof window === 'undefined' to the conditional expression with writeHead. Now it looks like this:

  if (!response.ok && typeof window === 'undefined') {
    res.writeHead(302, { Location: '/notes' })
    res.end()
    return {props: {}}
  }

I am honestly now positive why you need this though.
@juicecultus

from nextjs-course-app.

aashiqahmed98 avatar aashiqahmed98 commented on June 21, 2024

Instead of strict equality (===) operator use Object,is() and le me know!

from nextjs-course-app.

slavo3dev avatar slavo3dev commented on June 21, 2024

I had a similar issue, but I was trying to do all with ts. When I convert all to js everything was working as should be

from nextjs-course-app.

armor009 avatar armor009 commented on June 21, 2024

Can anyone explain what exactly is happening here?
This seems to cover intricacies of next.js and server responses.

from nextjs-course-app.

JasonNoonan avatar JasonNoonan commented on June 21, 2024

Having the same error. However, if I log params as the first line of getServerSideProps, the function works. Some kind of timing issue where params is not ready when fetch tries to call it, maybe?

export async function getServerSideProps({params, req, res}) {
  console.log(params)
  const response = await fetch(`http://localhost:3000/api/note/${params.id}`)

  if (!response.ok) {
    res.writeHead(302, { Location: '/notes' })
    res.end()
    return {props: {}}
  }

  const {data} = await response.json()
  
  
  if (data) {
    return {
      props: {note: data}
    }
  }
}```

from nextjs-course-app.

BhumilModi avatar BhumilModi commented on June 21, 2024

try this it worked for me
export async function getServerSideProps({ params, req, res }) {
const response = await fetch(http://localhost:3000/api/note/${params.id});

if (!response.ok) {
return res.writeHead(302, { Location: "/notes" }).end();
}

const { data } = await response.json();

if (data) {
return {
props: { note: data },
};
}
}

from nextjs-course-app.

Related Issues (4)

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.