Sails.js flash消息供用户注册 [英] Sails.js flash message for user registration
问题描述
然而,我遇到了一个小问题。在教程中,作者使用用户文件夹中的注册文件,并在用户控制器中分配路由。然后他使用 flash
向用户发送验证错误。
However I ran into a small problem. In the tutorial the author uses registration files inside his user folder and assigns routes in the user controller. He then sends validation errors using flash
to the user.
但是在我的项目中,注册文件位于根文件夹,我从routes.js文件中分配路由,如下所示
However in my project, the registration files lies in the root folder and I assign the routes from the routes.js file like so
module.exports.routes = {
'/': {
view: 'index'
},
'/register': {
view: 'register'
}
};
现在的问题是使用Flash向用户显示注册时的验证错误。我在用户控制器(创建)中使用了以下内容,并且似乎不起作用。
Now the problem is to using flash to show users the validation errors while registration. I have used the following in the user controller (create) and it doesn't seem to work
if (err){
err.success = 0;
console.log(err);
req.session.flash = {
err: err
}
req.flash('error', req.session.flash);
return res.redirect('/register');
}
任何建议?
Sails.js版本< 0.10.x根据他的其他线程输出此处
Sails.js version < 0.10.x based on his other thread output here
推荐答案
编辑:参见Sails文档 here 了解更多信息 - 基本上,由于您使用静态路由,因此在呈现视图之前不应用任何策略,因此闪存策略不起作用。我建议在您的 UserController
中添加注册
操作,只需调用 res.view( )
从那里。还有一个 StackOverflow post ,讨论了这一点,如果你想要更多的信息。
See the Sails documentation here for more information - basically, since you're using static routing, no policies are applied before rendering the view, hence the flash policy isn't working. I'd recommend adding a register
action in your UserController
and just calling res.view()
from there. There's also a StackOverflow post that discusses this, if you want more information.
我有一个我为自己的项目开发的替代品,你可以尝试(也需要非静态路由)。
I do have an alternative I developed for my own project that you can try out (also requires non-static routing).
在您的 api / policies
文件夹中,创建一个策略 flash.js
In your api/policies
folder, create a policy flash.js
:
// flash.js policy
module.exports = function(req, res, next) {
res.locals.messages = { success: [], error: [], warning: [] };
if(!req.session.messages) {
req.session.messages = { success: [], error: [], warning: [] };
return next();
}
res.locals.messages = _.clone(req.session.messages);
// Clear flash
req.session.messages = { success: [], error: [], warning: [] };
return next();
};
此策略允许三种不同的闪存类型:成功,警告和错误。它将为每个会话创建一个空字典,并在页面加载时清除它。
This policy allows for three different flash types: success, warning, and error. It'll create an empty dictionary for each session and clear it on page loads.
我创建了一个服务 FlashService.js
在 api / services
中以更容易地发送消息:
I created a service FlashService.js
in api/services
to more easily flash messages:
// FlashService.js
module.exports = {
success: function(req, message) {
req.session.messages['success'].push(message);
},
warning: function(req, message) {
req.session.messages['warning'].push(message);
},
error: function(req, message) {
req.session.messages['error'].push(message);
}
}
然后,在你的配置/policies.js
配置文件,请确保将 flash
策略分配给要使用闪存的控制器操作:
Then, in your config/policies.js
config file, make sure to assign the flash
policy to the controller actions that you want to use flash with:
// config/policies.js
module.exports.policies = {
'*': [true, 'flash'],
'UserController': {
'register': ['flash'],
// any future actions that want flash
},
'AnotherController': {
'someAction': ['flash', 'somePolicy'],
}
}
要在注册
操作中闪烁一条消息,只需使用 FlashService.success(req,message)
。或者,您可以使用 FlashService.error
和 FlashService.warning
,具体取决于您的UI样式如何工作,以及您想要区分你的Flash消息。
To flash a message in your register
action, just use FlashService.success(req, message)
. Alternatively, you can use FlashService.error
and FlashService.warning
, depending on how your UI styling works and how much you want to differentiate your flash messages.
在你看来,你可以这样写:
In your view, you can put something like this:
<% if (messages && messages['error'].length > 0) { %>
<div class="alert alert-danger">
<% messages['error'].forEach(function(message) { %>
<%= message %>
<br>
<% }); %>
</div>
<br>
<% } %>
<% if (messages && messages['warning'].length > 0) { %>
<div class="alert alert-warning">
<% messages['warning'].forEach(function(message) { %>
<%= message %>
<br>
<% }); %>
</div>
<br>
<% } %>
<% if (messages && messages['success'].length > 0) { %>
<div class="alert alert-success">
<% messages['success'].forEach(function(message) { %>
<%= message %>
<br>
<% }); %>
</div>
<br>
<% } %>
当然,您必须更改 div
类到任何类与您的UI相关。
Of course, you'll have to change the div
classes to whatever classes are relevant to your UI.
这篇关于Sails.js flash消息供用户注册的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!