我有一个表单提交过程,我有一个带有 if/else 语句的中间件,在我的身份验证控制器被触发之前验证值是否存在,但由于某种原因,我的复选框输入字段ERR_TOO_MANY_REDIRECTS
在表单提交时触发,甚至当我尝试加载表单时提交后的页面。我undefined
检查的原因是因为我注意到如果未检查该值,则正文不包含此输入。是否有更好的条件来检查是否选中或未选中此值,以消除我看到的重定向循环?有没有办法将此中间件设置为仅在路由的 POST 部分触发?
我使用的中间件/sign-up
:
siteRoutes.use('/sign-up', function(req, res, next){
console.log("Sign Up Use")
console.log(req.body);
models.User.findOne({
where: {
email: req.body.email
}
}).then(function(existingUser) {
if (existingUser){
req.flash('error', 'Email already exists.');
return res.redirect('/sign-up');
} else if (req.body.termsOfService === undefined) {
console.log("TOS Check")
req.flash('error', 'Accept terms');
return res.redirect('/sign-up');
} else {
console.log("Next")
next();
}
});
});
输入字段:
<input type="text" class="form-control" id="sign-up-fist-name" name="firstName" value="" placeholder="First Name">
<br />
<input type="text" class="form-control" id="sign-up-last-name" name="lastName" value="" placeholder="Last Name">
<br />
<input type="text" class="form-control" id="sign-up-username" name="email" value="{{user.email}}" placeholder="Email Address">
<br />
<input type="password" class="form-control" id="sign-up-password" name="password" value="" placeholder="Password">
<br />
<input type="password" class="form-control" id="sign-up-confirm-password" name="confirmPassword" value="" placeholder="Confirm Password">
<br />
<label>
<input type="checkbox" name="termsOfService"> I confirm that I have read, consent and agree to Synotate's <a href="/terms-of-service">Terms of Service</a> and <a href="privacy-policy">Privacy Policy</a>
</label>
使用req.method
以确定该请求是否是GET
或POST
。
更改req.body.termsOfService === undefined
为req.method === 'POST' && req.body.termsOfService === undefined
。
另一种方法是使用siteRoutes.get
andsiteRoutes.post
而不是siteRoutes.use
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句