I am building a node Js project and i am saving the values of form to a mongoDB database.
Despite of trying i couldn't find what is causing this error.
Error is at router.post
function on 3rd line.
Please guide me through this through your magical powers of coding and debugging. :D
const express = require('express');
const router = express.Router();
const Employee = require('../models/employee');
router.get('/',(req, res) => {
res.render('index');
});
router.get('/employee/new', (req, res) => {
res.render('new');
});
router.post('/employee/new', (req, res) => {
let newEmployee = {
name : req.body.name,
designation : req.body.designation,
salary : req.body.salary
}
Employee.create(newEmployee).then(employee => {
res.redirect('/');
}).catch(err => {
console.log(err);
});
});
module.exports = router;
you can see clearly I have defined the newEmployee
Object, so why 'name' is the property of undefined.
<div class="container mt-5 w-50">
<h2 class="mb-4">Add New Employee</h2>
<form action="/employee/new" method="POST">
<input type="text" name="name" class="form-control" placeholder="Employee Name">
<input type="text" name="designation" class="form-control" placeholder="Employee Designation">
<input type="text" name="salary" class="form-control" placeholder="Employee Salary">
<button type="submit" class="btn btn-danger btn-block mt-3">Add to Database</button>
</form>
</div>
question from:
https://stackoverflow.com/questions/66064389/getting-typeerror-cannot-read-property-name-of-undefined-while-posting-the-f 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…