2017-03-07 48 views
1

我想通過使用Sequelize來更新記錄表格。 不幸的是,我試圖更新的事件的ID似乎是未定義的。我如何正確發送它?如何使用Sequelize更新記錄?

我在我的控制器的代碼塊看起來是這樣的:

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)); 

}); 

雖然,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> 
+0

action =「/ events/edit」in html but your api route is/edit –

+0

@AsifSaeed可能是那個潰敗呃被另一個路由器命名('/ events')。 –

+0

@IonicăBizău有沒有提交在HTML也是正確的?加上沒有標籤來容納eventid需要通過,也許應該有一個隱藏的標籤 –

回答

2

因爲req.body不包含eventid(它不是從客戶端傳遞的),所以您獲得eventid作爲undefined。要從客戶端傳遞它,您必須添加具有name="eventid"屬性的輸入。

在EJS模板,你需要渲染eventid值作爲hidden輸入(<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> 
+0

非常感謝!它現在正常工作! –