Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

355
Views
TypeError: res.json is not a function (not able to get fetch data)

I m trying to GET response using fetch API not stuck in a error as I mentioned below. Here's my code

const DefaultLayout = () => {
      let history = useHistory()
      const callHomePage = async () => {
        try {
          const res = fetch('http://localhost:4000/api/authenticate', {
            method: 'GET',
            headers: {
              Accept: 'application/json',
              'Content-type': 'application/json',
            },
            credentials: 'include',
          })
          console.log(res)
          const data = await res.json()
          console.log(data)
          if (!res.status === 200) {
            const error = new Error(res.error)
            throw error
          }
        } catch (err) {
          console.log(err)
          history.push('login')
        }
    }

Error: TypeError: res.json is not a function Promise {} shows pending

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

const DefaultLayout = () => {
  let history = useHistory()
  const callHomePage = async () => {
    try {
      const res = await fetch('http://localhost:4000/api/authenticate', {
        method: 'GET',
        headers: {
          Accept: 'application/json',
          'Content-type': 'application/json',
        },
        credentials: 'include',
      })
      console.log(res)
      const data = await res.json()
      console.log(data)
      if (!res.status === 200) {
        const error = new Error(res.error)
        throw error
      }
    } catch (err) {
      console.log(err)
      history.push('login')
    }
}
about 4 years ago · Juan Pablo Isaza Report

0

You need to await the fetch statement and then call the .json method of the response.

const res = await fetch(...)

data = await res.json();

Read more: https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!