2017-03-07 2 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

조치는 = "/ 이벤트/편집"HTML에서하지만 API 경로가/편집 –

+0

그 완패 아마 @AsifSaeed입니다 다른 라우터 ('/ events')가 네임 스페이스를 지정합니다. –

+0

@ IonicăBizău 거기에 n뿐만 아니라 HTML 제출? 더하기 거기에 전달해야하는 eventid를 보유 할 태그가 없을 수도 있습니다. 아마도 숨겨진 태그가 있어야합니다. –

답변

2

eventid이 없으므로 (클라이언트 쪽에서 전달되지 않았기 때문에) eventidundefined입니다. 클라이언트 쪽에서 전달하려면 name="eventid" 특성을 가진 입력을 추가해야합니다. EJS 템플릿에서

당신은 hidden 입력으로 eventid 값을 렌더링 할 필요가 ( <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

정말 고마워요! 이제 제대로 작동합니다! –

관련 문제