error in creating schema for login registration

106 Views Asked by At

I am trying to create a login registration using hbs , node js and mongodb . I am getting an unexpected error of undefined

hbs form

 <form id="form_register" action="/register" method="post">
                    <input type="text" name="name" placeholder="FullName" id="fullname" required>
                    <input type="text" name="email" placeholder="Email Address" id="email" required>
                    <input type="password" name="password" placeholder="Password" id="password" required>
                    <div id="check_box_term">
                        <input type="checkbox" id="checkbox" checked>
                        <label for="" id="checkbox_p">Accept Teams and Conditions.</label>
                    </div>
                    <button type="submit" id="submit_register">Sign up my Account</button>
                </form>

js code router file code


 Router.post('/register',async(req,res)=>
 {
     try
     {
         const{
             name,
             email,
             password
         }=req.body;
         console.log(name);
     }
     catch(err)
     {
        console.log(err);
     }
 })

mongo schema code

const mongoose = require('mongoose');
const schema = mongoose.Schema;

const userSchema = new schema({
    name:{
        type:String,
        required:true
    },
    email:{
        type:String,
        unique:true,
        required:true
    },
    password:{
        type:String,
        required:true
    },
})

module.exports = mongoose.model('loginRegister' , userSchema);

error i am facing is

TypeError: Cannot destructure property 'name' of 'req.body' as it is undefined.
    at E:\FULL_WEBSITE_PROJECT\routers\routes.js:28:14
    at Layer.handle [as handle_request] (E:\FULL_WEBSITE_PROJECT\node_modules\express\lib\router\layer.js:95:5)
    at next (E:\FULL_WEBSITE_PROJECT\node_modules\express\lib\router\route.js:144:13)
    at Route.dispatch (E:\FULL_WEBSITE_PROJECT\node_modules\express\lib\router\route.js:114:3)
    at Layer.handle [as handle_request] (E:\FULL_WEBSITE_PROJECT\node_modules\express\lib\router\layer.js:95:5)
    at E:\FULL_WEBSITE_PROJECT\node_modules\express\lib\router\index.js:284:15  
    at Function.process_params (E:\FULL_WEBSITE_PROJECT\node_modules\express\lib\router\index.js:346:12)
    at next (E:\FULL_WEBSITE_PROJECT\node_modules\express\lib\router\index.js:280:10)
    at Function.handle (E:\FULL_WEBSITE_PROJECT\node_modules\express\lib\router\index.js:175:3)
    at router (E:\FULL_WEBSITE_PROJECT\node_modules\express\lib\router\index.js:47:12)
1

There are 1 best solutions below

4
On

You need to install bodyParser

npm install body-parser

Then require it

const bodyParser = require('body-parser')

After that, use it like this

app.use(bodyParser.json()) // for parsing application/json
app.use(bodyParser.urlencoded({ extended: true }))