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

143
Views
How do i post to mongodb database ? I have tried this but it still thrwoing error
const data = { name, price, quantity, image, desc, sup_name, email }
    fetch('https://gentle-plateau-90897.herokuapp.com/fruits', {
        method: 'POST',
        headers: {
            'content-type': 'application/json'
        },
        body:(data)
    })
        .then(res => res.json())
        .then(data => {
            console.log(data)
            window.alert('Item added')
            e.target.reset()
        })

I am trying to post a method to send data to my MongoDB database, but it throwing an error saying 'Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0'

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

0

Unexpected token < in JSON at position 0 means you are getting a HTML response instead of json response and you are trying to convert it to json. Check the response body in network tab in the dev tool. It might return HTML because you didn't include Accept: application/json as header or some error happened.

about 4 years ago · Juan Pablo Isaza Report

0

The error is coming due to the conversion of the res you are getting from the API. The res is something that can't be converted to valid JSON.

From the error, it seems that the res might be an HTML string and converting it to JSON will always yield an error. To use it, you should use innerHTML in the view.

const data = { name, price, quantity, image, desc, sup_name, email }
    fetch('https://gentle-plateau-90897.herokuapp.com/fruits', {
        method: 'POST',
        headers: {
            'content-type': 'application/json'
        },
        body:(data)
    })
        // .then(res => res.json()) // remove this line for now
        .then(data => {
            /**
            check here what type of data you are getting 
            is it valid JSON or not?
            */
            console.log(data) 
            window.alert('Item added')
            e.target.reset()
        })
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!