简体   繁体   中英

Can't get the values from database while editing post mongodb nodejs express handlebars

Im currently seeking a course from udemy on node js, so I've been working on a CRUD application using NodeJS, MongoDB, Mongoose, Express, Express-Handlebars. Whenever i try to edit the post i cannot get the values on the edit form...some help would be appreciated

I've searched and searched for every possible solution i could find but nothing worked!

NodeJs code to go to edit page when button clicked

//Ideas Edit
app.get("/ideas/edit/:id", (req, res) => {
  Idea.findOne({
    _id: req.param.id
  }).then(idea => {
    res.render("ideas/edit", {
      idea: idea
    });
  });
  // .catch(err => console.log(err));
});

Edit form page

<div class="card card-body">
    <h1>Edit Video Idea</h1>
    <form action="/ideas" method="POST">
        <div class="form-group">
            <label for="title">Title</label>
            <input type="text" name="title" class="form-control" value="{{idea.title}}" />
        </div>
        <div class="form-group">
            <label for="details">Details</label>
            <input type="text" name="details" class="form-control" value="{{idea.details}}" />
        </div>
        <button class=" btn btn-primary">Submit</button>
    </form>
</div>

I cannot get the values of the current post being edited in the form "value="{{idea.title}}". Its correct as the tutorial but not working

如果您使用的是ejs模板,那么您可以这样写:

<input type="text" name="title" class="form-control" value="<%=idea.title%>" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM