2014-06-22 1 views
0

Node.js, Express 및 Connect-Flash에서 플래시 메시지를 표시하는 데 문제가 있습니다.Node.js 유형 오류 connect-flash

예를 들어 오류 메시지가 표시 될 수 있지만 하나 이상의 메시지 (예 : 업데이트가 성공하면 성공 메시지, 업데이트에 문제가있는 경우 오류 메시지)를 보내려는 경우 문제가 발생합니다.

다음은 오류입니다 :

http://www.evernote.com/shard/s15/sh/5160abda-ddac-405c-9ea5-3563f3c39a02/7576bce006e47a8b1ea0346472996550

그리고 여기 내 코드의 조각을이다 :

// Routes.js 

// ====================================== 
    // CHANGE PASSWORD 
    // ====================================== 
    app.get('/password', isLoggedIn, function (req, res) { 

     res.render('password', { 
      user: req.user, // get the user out of session and pass to template 
      error: req.flash('error'), 
      success: req.flash('success') 
     }); 
    }); 

app.post('/password', isLoggedIn, function (req, res) { 

     // Save settings 
     if (req.body.newPassword == req.body.confirmNewPassword) { 

      User.findOne({_id: req.user._id}, function (err, user) { 

       user.local.password = generateHash(req.body.newPassword); 

       user.save(function(err) { 

        if (err) 
         throw new Error('There was a problem saving the password'); 

        req.flash('success', 'Password updated successfully') 
        req.user = user; 
        res.redirect('password'); 
       }); 

      }); 

     } else { 
      req.flash('error', 'Passwords did not match'); 
      res.redirect('password'); 
     } 
    }); 

// Jade template 
// =================== 

- if (error) // GET ERROR ON THIS LINE <------------ 
    div.alert.alert-warning #{error} 
- elseif (success) 
    div.alert.alert-success #{success} 

감사합니다!

답변

0

문제가 해결되었습니다. 여기에 내가했던 일이야 :

routes.js

// ====================================== 
// CHANGE PASSWORD 
// ====================================== 
app.get('/password', isLoggedIn, function (req, res) { 

    var flash = req.flash(); 

    res.render('password', { 
     user: req.user, 
     flash: flash 
    }); 
}); 

password.jade

if flash.error 
     div.alert.alert-danger #{flash.error} 
    else if flash.success 
     div.alert.alert-success #{flash.success}