0

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:
  • Hi and welcome to Stack Overflow. Where is `id` made a property of the handlebars view object used to render the document sent to the client for an existing user ? Also, where is the "edit" button used to edit user details? – traktor Apr 04 '20 at 03:27
  • The edit button is added to another page named list.hbs. The records are listed properly everything is working fine but could not get the submitted information in form fields when I click edit – october cms Apr 04 '20 at 04:05
  • Take a look at https://stackoverflow.com/a/60534298/6585238 – nanquim Apr 04 '20 at 17:04

3 Answers3

1

I think, if you are using mongoose and newer versions of express-handlebars, it can be solved by using lean() method after find() like:

User.find().lean()

https://stackoverflow.com/a/60368203/7149874

umess
  • 31
  • 4
0

I thought the problem was in passing the value from the handlebars but it was with the version of the handlebar.This solved my problem:

npm install handlebars@4.5.3
0
npm install @handlebars/allow-prototype-access

*after the installtion of above comment,go to the site given below and paste the code (Usage (express-handlebars and mongoose))
https://www.npmjs.com/package/@handlebars/allow-prototype-access.

Anjas K
  • 1
  • 1
  • If we don't install npm install @handlebars/allow-prototype-access package,that will show error in any version . I think the newest version of mongoose along with handlebars should need this package. – Anjas K Aug 21 '20 at 09:30