Unexpected end of JSON input in Next.js

Viewed 2458

I'm fetching data from /api/notes/1 and receive this:

{
"id":1,
"author":1,
"title":"Pierwsza notka",
"excerpt":"Taka tam notka, bla bla bla",
"body":"Pirwsza notka elo",
"private":1,
"created_at":"2021-04-07T12:59:59.000Z",
"updated_at":"2021-04-07T12:59:59.000Z"
}

Which is fine and dandy, but when I pass it into Next's getStaticProps:

export async function getStaticProps({ params }) {
  const res = await fetch(`${config.apiURL}/notes/${params.id}`);
  const post = await res.json();

  return { props: { post } };
}

It returns an error:

FetchError: invalid json response body at http://localhost:3000/api/notes/1 reason: Unexpected end of JSON input

What's going on here?

1 Answers

The problem was my fault. The fetch returned 401 Unauthorized with no body.

Related