skip to Main Content

When I click Submit to post the entry on MongoDB, it gives me this error
MongoDB is connected to the server as well.

This is my router file

const express = require('express');
const Club  = require('../models/club')
const Router = express.Router();


Router.get('/', (eror,res)=>{
    res.render('index')
})

Router.post('/form', (req,res)=>{
    const name = req.body.name;

   console.log(name);
})

module.exports = Router;

This is my form

<form action="/form" method="post">
 <input type="text" name="name" placeholder="Add an Entry">
 <input type="submit" value="Submit">
</form>

I tried everything, but it wont work

3

Answers


  1. We have to add middleware to parse request body. In your case:

    Router.use(express.urlencoded({ extended: true }))
    
    Login or Signup to reply.
  2. Depending on your Express version, you might need to use use the bodyParser middleware before your handlers

    const express = require('express');
    var bodyParser = require('body-parser')
    
    const app = express();
    app.use(bodyParser.urlencoded());
    ...
    
    Login or Signup to reply.
  3. In your app.js file you need to add a middleware to parse the data, you don’t need to install bodyParser at the current express version they already integrated this feature, you just need to add this:

    const express = require('express');
    const app = express();
    ...
    app.use(express.urlencoded({ extended: true }));  // this line for your case
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search