I am trying to get the entered details by the user in the form fields so that I can edit and update the selected records. But, when I click the edit button I am getting blank form fields in the form.
router.get('/',function(req,res){
res.render('./users/addoredit',{
viewTitle:"Insert User",
user:req.body
})
})
router.post('/',function(req,res){
if(req.body._id==="")
insertRecord(req,res)
else
updateRecord(req,res)
})
function insertRecord(req,res){
const user=new User()
user.name=req.body.name
user.address=req.body.address
user.email=req.body.email
user.mobile=req.body.mobile
user.save((err,docs)=>{
if(!err)
res.redirect('user/list')
else
res.render('./users/addoredit',{
viewTitle:"Insert User",
user: req.body})
})
}
router.get('/list',(req,res)=>{
User.find((err,docs)=>{
if(!err){
res.render('./users/list',{
list:docs
})
}
else{
console.log(err)
}
}).lean()
})
function updateRecord(req,res){
User.findOneAndUpdate({_id:req.body._id},req.body,{new:true},(err,docs)=>{
if(!err)
res.redirect('/user/list')
else
console.log
})
}
router.get('/:id',function(req,res){
User.findById(req.params.id,(err,doc)=>{
if(!err)
res.render('./users/addoredit',{
viewTitle:"Update User",
user:doc
})
else
res.redirect('user/list')
})
})
I am getting the value passed by inserting the record in addoredit handlebar as follows:
<form action="/user" method="POST" autocomplete="off">
<div class="form-group">
<input type="hidden" name="_id" value="{{user._id}}">
<label for="name">Name</label>
<input type="text" class="form-control" name="name" placeholder="Enter name" value="{{user.name}}">
</div>
<div class="form-group">
<label for="address">Address</label>
<input type="text" class="form-control" name="address" placeholder="Enter address" value="{{user.address}}">
</div>
<div class="form-group">
<label for="email">Email address</label>
<input type="email" class="form-control" name="email" placeholder="Enter email" value="{{user.email}}">
</div>
<div class="form-group">
<label for="mobile">Mobile</label>
<input type="number" class="form-control" name="mobile" placeholder="Enter mobile number" value="{{user.mobile}}">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
This is the first time I am trying a project in nodejs so, I am not able to get what went wrong. I have passed the user entered value as req.body. All the operations are working properly but could not update the records as I am not able to get the user id from the form.
The hbs to edit and delete the records after listing:
<table class="table table-striped">
<thead>
<tr>
<th>Full Name</th>
<th>Email</th>
<th>Mobile</th>
<th>City</th>
<th></th>
</tr>
</thead>
<tbody>
{{#each list}}
<tr>
<td>{{this.name}}</td>
<td>{{this.address}}</td>
<td>{{this.email}}</td>
<td>{{this.mobile}}</td>
<td>
<a href="/user/{{this._id}}"><i class="fa fa-pencil fa-lg"></i></a>
<a href="/user/delete/{{this._id}}" onclick="return confirm('Are you sure to delete this record ?');"><i class="fa fa-trash fa-lg" aria-hidden="true"></i></a>
</td>
</tr>
{{/each}}
</tbody>
I am getting the following error which I had not noticed earlier:
Handlebars: Access has been denied to resolve the property "_id" because it is not an "own property" of its parent.
You can add a runtime option to disable the check or this warning: