NicLovin
NicLovin

Reputation: 355

Data from form not showing up in request.body

I am trying to insert data from a input form into a database using express and node-pg.

Here is the code for my input form:

<div class="col" id="inputform">
   <h2 style="margin-bottom: 3%;">Line Item Input</h2>
   <form action="/dbinsert" method="post">
      <div class="form-row">
         <div class="form-group col-md-6">
            <label for="wo_num">Work Order Number:</label>
            <input type="text" class="form-control" id="wo_num" name="wo_num" placeholder="WO #">
            <label for="description">Description:</label>
            <input type="text" class="form-control" id="description" name="description" placeholder="Description">
         </div>
         <div class="form-group col-md-6">
            <label for="qty">Quantity:</label>
            <input type="text" class="form-control" id="qty" name="qty" placeholder="QTY">
            <label for="cost">Cost:</label>
            <input type="text" class="form-control" id="cost" name="cost" placeholder="Cost $">
         </div>
      </div>
      <button type="submit" class="button">Submit</button>
   </form>
</div>

Here is how I handle the http post request after the submit button is clicked (I am just trying to access the values from the form right now):

const insertLineItemDB = (request, response) => {
    
    // attempt to access values from the form
    console.log(request.body.wo_num);
    console.log(request.body);

   response.status(201).send("testing");
}

After running, my console displays this:

undefined
{}

Why is request.body empty? How do I access the values input in the form?

Upvotes: 0

Views: 698

Answers (1)

mohammad Naimi
mohammad Naimi

Reputation: 2359

add this line where express defined and imported

app.use(express.json({  extended: true }));
app.use(express.urlencoded({  extended: true }));

Upvotes: 3

Related Questions