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

391
Vistas
Javascript object coming through empty on server side

I have a client-side script running to send the string "Y" to the server. I set up a console.log on the client-side (which you can see below) and another on the server-side. The one on the client-side works, but the one logs an "empty" object.. it just shows "{}". How do I get my data to stay in the object?

                const status = "Y";

                const options = {
                    method: 'POST',
                    headers: {
                        'Content-type': 'application/json'
                    },
                    body: status
                fetch('/events/<%- event.id %>/prompt', options)

                console.log(options.body)

Here's my route for context:

router.route('events/:id/prompt')
    .get(catchAsync(events.showPrompt))
    .post(catchAsync(events.checkIn))

And my controller:

module.exports.checkIn = async(req, res) => {
    console.log(req.body);
}

How do I get the object to come through to the server?

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

For sending "y" as the content and receiving that in Express, you need two things:

  1. You need to make sure the content-type is set to text/plain on the request.

  2. You need the appropriate middleware that will read that text/plain body.

    app.use(express.text())

Then, you will find the body in req.body within any express request handler registered after the above middleware.


You could pick different content-types also such as application/json, the corresponding middleware for that content-type app.use(express.json())` and then format the body data in that format.


It's important to realize that Express does not by itself read the body of an incoming request. It reads the headers, but not the body by default. If you want the body to be read, then you need middleware that is looking for whatever content-type the incoming request has, reads the body, parses it from whatever it's format is and puts the resulting parsed data into req.body. Express comes with a number of built-in middleware for popular content-types.

about 4 years ago · Juan Pablo Isaza Denunciar

0

Status is a string. However body have to take a object with key-value pair. If send like with like below, then you get object which contains status on the backend side.

body: {status: status}
about 4 years ago · Juan Pablo Isaza Denunciar

0

Problem from :

  • Client : you choose Content-type': 'application/json' , so your body must be json format , something like body : { status } . Make sure you sent exact object with browser debug , because some call api package can change value of request.
  • Server : Some nodejs framework need parse the value is sent from client before read it (Exp : app.use(express.json()) with Express)
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