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

394
Views
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 answers
Answer question

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 Report

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 Report

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 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!