Next.js Discord

Discord Forum

Getting weird error when doing fetching data

Answered
Bighead carp posted this in #help-forum
Open in Discord
Bighead carpOP
I'm trying to get familiar with Next.js 13 workflow. I'm doing a simple API call inside the "api/route.ts" file in my App directory and I'm getting the error in the picture.

Any ideas on how to solve this, please?
Answered by Northern Wheatear
That is because in the error that you received in the request returning 404, the api was returning HTML as the error response instead of json. And you are returning Response.json with the data so this HTML cannot be converted to json and hence the error
View full answer

3 Replies

Bighead carpOP
My fetching code inside /api/route.ts

export async function getWord(word) {
  const res = await fetch(`https://api.dictionaryapi.dev/api/v2/entries/en/${word}`)
  const data = await res.json()

  return Response.json({ data })
}
by the way, the app doesn't crash with the error. It stills fetches correctly, but the error is bugging me so I want to fix it.
Northern Wheatear
That is because in the error that you received in the request returning 404, the api was returning HTML as the error response instead of json. And you are returning Response.json with the data so this HTML cannot be converted to json and hence the error
Answer