Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

392
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda