deejay123
deejay123

Reputation: 89

Receiving error Unexpected token < in JSON at position 0; is this a backend or frontend problem?

I currently have a PATCH method:

export const patchEvent = (eventToUpdate) => dispatch => {
  fetch(`http://localhost:3000/events/${eventToUpdate.id}/`, {
      method: "PATCH",
      headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json'
      },
      body: JSON.stringify({
        workers_hired: eventToUpdate.workers_hired
      })
  })
    .then(res => res.json())
    .then(data =>{
      dispatch({
        type: PATCH_EVENT,
        payload: eventToUpdate.workers_hired
      })
    })
}

Which receives data from a React event:

  const addWorker = () => {
    const viewedEvent = props.rawEvents.find(event => {
      return event.name === props.event.title
    })

    viewedEvent.workers_hired = 5

    props.patchEvent(viewedEvent)
  }

Where props.patchEvent is my Redux action that is shown in the first code snippet.

When I fire this action I receive a 404 error "Unexpected token < in JSON at position 0"

Now, I understand that this has to do with the fetch response not being in correct JSON but I cannot for the life of me figure out where the formatting has gone wrong in my code. Any help would be greatly appreciated!

Upvotes: 0

Views: 68

Answers (1)

Alex
Alex

Reputation: 46

Error 404 means that the url you requested not found. Check if you fetch the correct url.

Upvotes: 1

Related Questions