2017-03-07 7 views
1

Sequelizeを使用してレコードフォームを更新しようとしています。 残念ながら、私が更新しようとしているイベントのIDは未定義です。どうすれば正しく送信できますか?Sequelizeを使ってレコードを更新するには?

私は私のコントローラを持っているコードのブロックは、次のようになります。

<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> 
:イベントを編集するときに、ユーザーが値を紹介するHTMLファイルは、次のようになります、が

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

}); 

+0

アクション= "/イベント/編集" が、あなたのAPIのルートおそらくその敗走を@AsifSaeed /編集 –

+0

です他のルータによって名前空間が指定されています( '/ events')。 –

+0

@IonicăBizăunはHTMLでも提出されていますか?さらに、渡される必要のあるeventidを保持するタグはありません。 –

答えて

2

にはeventidが含まれていないため、undefinedとしてeventidが得られます(クライアント側からは渡されません)。クライアント側から渡すには、name="eventid"属性を持つ入力を追加する必要があります。

あなたの形で添加し、このラインであることを行うことができますが hidden入力として eventid値をレンダリングする必要がEJSテンプレート( <input type="hidden" ...

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

これは、更新の形でありますコード:HTMLで

<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

ありがとう!それは今正しく動作します! –

関連する問題