Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

136
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda