【发布时间】:2016-10-30 08:45:39
【问题描述】:
我正在尝试弄清楚如何将 express-validator 与 angularjs 和 html 一起使用来显示验证错误,我的问题是我无法访问 errors 变量。请参阅下面的代码。
routes.js
router.post('/register', function(req, res){
var name = req.body.name;
var email = req.body.email;
var username = req.body.username;
var password = req.body.password;
var password2 = req.body.password2;
req.checkBody('name', 'Name is required').notEmpty();
req.checkBody('email', 'Email is required').notEmpty();
req.checkBody('email', 'Email not valid').isEmail();
req.checkBody('username', 'Username is required').notEmpty();
req.checkBody('password', 'Password is required').notEmpty();
req.checkBody('password2', 'Passwords does not match').equals(req.body.password);
var errors = req.validationErrors();
if(errors){
res.render('register.html', {
errors: errors
});
} else {
console.log('no errors');
}})
我在我的 server.js 中发现了问题所在。 这似乎导致了快速路由和角度路由之间的问题,使快速路由失败,因此无法按需要传递数据。见下文。
app.use(function(req, res){
res.sendFile(__dirname + '/app/index.html');
});
app.use('/', routes);
app.use('/user', users);
如果我尝试这样做,请参见下文。
app.use('/', routes);
app.use('/user', users);
app.use(function(req, res){
res.sendFile(__dirname + '/app/index.html');
});
这使得快速路由可以按我的意愿传递数据,但我的角度路由似乎有问题,一旦我刷新页面 - 视图无法正确加载。
但是,我确实知道如何使用 ejs 或车把来显示它,但这不符合我的需要,应该有一种方法可以用 angular 存储错误并以某种方式用 html 显示它对吗?
感谢任何帮助!
【问题讨论】:
-
您应该能够使用 res.json({errors:errors}) 进行响应,并在 ng-repeat 的角度侧使用错误数组。您是否面临这方面的问题?
-
@TalhaAwan 非常感谢,但我通过传入 ng-repeat="err in errors" 进行了尝试,它没有获取任何内容:/
标签: javascript html angularjs node.js express