2017-08-04 92 views
1

我試圖去適應我的錯誤代碼,因此它也可以處理其他類型的錯誤一樣成功使用快速閃光與EJS

例如,當我使註冊網頁

res.render('signup', { 
errors: req.flash('errors') 
}); 

在提交頁面,我成功地發送錯誤閃存與此代碼:

req.flash('errors', { msg: 'Account with that email address already exists.' }); 

並使用代碼正確呈現它。

<% if (errors.length > 0) { 
     for(var i=0; i < errors.length; i++) { %> 
    <div class="alert alert-danger alert-dismissible fade show" role="alert"> 
     <button type="button" class="close" data-dismiss="alert" aria- 
    label="Close"> 
      <span aria-hidden="true">&times;</span> 
     </button> 
     <%= errors[i].msg %> 
    </div> 
    <% } %> 
     <% } %> 

但在其他情況下,我可能有

req.flash('success', { msg: 'You have signed up successfully' }); 

你將如何適應的代碼閃存渲染塊的上方,它同時適用於「錯誤」和「成功」

我發現一些.pug代碼(不是EJS)在線(sahat hackathon-starter),它檢查messages.errors,但我不確定消息來自哪裏!

if messages.errors 
    .alert.alert-danger.fade.in 
    button.close(type='button', data-dismiss='alert') 
     i.fa.fa-times-circle-o 
    for error in messages.errors 
     div= error.msg 
if messages.info 
    .alert.alert-info.fade.in 
    button.close(type='button', data-dismiss='alert') 
     i.fa.fa-times-circle-o 
    for info in messages.info 
     div= info.msg 
if messages.success 
    .alert.alert-success.fade.in 
    button.close(type='button', data-dismiss='alert') 
     i.fa.fa-times-circle-o 
    for success in messages.success 
     div= success.msg 

感謝 託尼

回答

0
<% if (errors.length > 0) { 
    for(var i=0; i < errors.length; i++) { %> 
    <div class="alert alert-danger alert-dismissible fade show" role="alert"> 
     <button type="button" class="close" data-dismiss="alert" aria-label="Close"> 
      <span aria-hidden="true">&times;</span> 
     </button> 
     <%= errors[i].msg %> 
    </div> 
    <% } %> 
     <% } %> 


     <% if (success.length > 0) { 
     for(var i=0; i < success.length; i++) { %> 
     <div class="alert alert-success alert-dismissible fade show" role="alert"> 
      <button type="button" class="close" data-dismiss="alert" aria-label="Close"> 
       <span aria-hidden="true">&times;</span> 
      </button> 
      <%= success[i].msg %> 
     </div> 
     <% } %> 
      <% } %>