2013-05-09 70 views
0

上顯示錯誤消息我使用Locomotivejs和passportJS對項目權威性,我發現了一些代碼網上辦理登記:PassportJS的NodeJS

AccountController.create = function() { 
    var account = new Account(); 
    console.log("test"); 
    account.email = this.param('email'); 
    account.password = this.param('password'); 
    account.name.first = this.param('first'); 
    account.name.last = this.param('name.last'); 

    var self = this; 
    account.save(function (err) { 
    if (err) { 
     console.log(err); 

     return self.redirect(self.urlFor({ action: 'new' })); 
    } 
    return self.redirect(self.urlFor({ action: 'login' })); 
    }); 
}; 

但我無法弄清楚如何在顯示錯誤消息如「用戶名已存在」或「密碼不匹配」等頁面,我只能讓他們到console.log()。有誰知道我該怎麼做?

回答

0

connect-flash是針對這種情況非常有用:

// in your app's config: 
var flash = require('connect-flash'); 
... 
app.use(flash()); 
... 

// in your #create controller: 
if (err) { 
    req.flash('error', 'YOUR ERROR MESSAGE HERE'); 
    return self.redirect(self.urlFor({ action: 'new' })); 
} 

// in your #new controller: 
res.render('TEMPLATE', { errors: req.flash('error') }); 

而在你的模板,檢查是否存在errors(它是一個數組)和渲染的消息。