如何使用Sequelize更新logging?

我正尝试使用Sequelize更新logging表格。 不幸的是,我试图更新的事件的ID似乎是未定义的。 我怎样才能正确地发送它?

我在我的控制器中的代码块如下所示:

router.get('/edit/:eventid', function(req, res) { var eventid = req.params.eventid; Event.findById(req.params.eventid).then(function(event) { eventid= event.eventid; title= event.title; description= event.description; availabletickets=event.availabletickets; date= event.date; newDate= date.toString(); newdate= newDate.substring(0,21); console.log(eventid); }) .then(function() { res.render('eventEdit', { eventid: eventid, pagetitle: ' Edit Event', title: title, description:description, availabletickets:availabletickets, newdate: newdate, }); }) .catch(function(error) { res.render('error', { error: error }); }); }); router.post('/edit', function(req, res){ var eventid = req.body.eventid; var title = req.body.title; var description = req.body.description; var availabletickets= req.body.availabletickets; var date = req.body.date; console.log( eventid); //this returns undefined. console.log('title, description, availabletickets, date); const newData = { title: title, date: date, description: description, availabletickets: availabletickets }; Event.update( newData, { where:{ eventid:eventid } } ).then(function() { console.log("Event updated"); res.redirect('/events'); }).catch(e => console.error(e)); }); 

尽pipe用户在编辑事件时引入值的HTML文件如下所示:

  <div class="container"> <h2><%= pagetitle %> </h2> <form method="post" action="/events/edit"> <div class="container"> <div class="col-md-12 "> <div class="row"> <div class="col-md-12 "> <input class="form-control" type="text" name="title" id="title" value="<%= title %>" placeholder="Titlu Eveniment" required="true"> <p style="color:#8B0000;"><small>*This field is mandatory</small></p> <textarea rows=7 class="form-control" type="text" name="description" id="description" placeholder="Descriere Eveniment"><%= description %></textarea> <br> <input class="form-control" type="text" name="availabletickets" id="availabletickets" value="<%= availabletickets %>"> <br> <label> Data:<%= newdate %> </label> <br/> <label>Cahnge event date:</label> <input class="form-control" type="date" name="date" id="date" style="width:190px;" ><br> <button class="btn" id="addValue" style="background-color:#8B0000; color:white;">Save</button>&nbsp; <button class="btn btn-warning">Cancel</button> </div> </div> </div> </div> </form> </div> 

       

网上收集的解决方案 "如何使用Sequelize更新logging?"

您将eventid undefinedundefined因为req.body不包含eventid (它不会从客户端传递)。 要从客户端传递它,您必须添加具有name="eventid"属性的input。

在EJS模板中,您需要将eventid值作为hiddeninput( <input type="hidden" ...

您可以通过在表单中​​添加以下代码来完成此操作:

 <input type="hidden" value="<%= eventid %>" name="eventid" /> 

这是更新的表单代码:

 <div class="container"> <h2><%= pagetitle %> </h2> <form method="post" action="/events/edit"> <input type="hidden" value="<%= eventid %>" name="eventid" /> <div class="container"> <div class="col-md-12 "> <div class="row"> <div class="col-md-12 "> <input class="form-control" type="text" name="title" id="title" value="<%= title %>" placeholder="Titlu Eveniment" required="true"> <p style="color:#8B0000;"><small>*This field is mandatory</small></p> <textarea rows=7 class="form-control" type="text" name="description" id="description" placeholder="Descriere Eveniment"> <%= description %> </textarea> <br> <input class="form-control" type="text" name="availabletickets" id="availabletickets" value="<%= availabletickets %>"> <br> <label> Data: <%= newdate %> </label> <br/> <label>Cahnge event date:</label> <input class="form-control" type="date" name="date" id="date" style="width:190px;"> <br> <button class="btn" id="addValue" style="background-color:#8B0000; color:white;">Save</button>&nbsp; <button class="btn btn-warning">Cancel</button> </div> </div> </div> </div> </form> </div>