gpt4 book ai didi

javascript - 防止复选框输入条件上的无限循环

转载 作者:行者123 更新时间:2023-12-03 00:08:27 25 4
gpt4 key购买 nike

我有一个表单提交过程,其中包含 if/else 语句的中间件正在验证在触发身份验证 Controller 之前是否存在值,但由于某种原因,我的复选框输入字段正在触发 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判断请求是否为GETPOST .

更改req.body.termsOfService === undefinedreq.method === 'POST' && req.body.termsOfService === undefined .

另一种方法是使用siteRoutes.getsiteRoutes.post而不是siteRoutes.use

关于javascript - 防止复选框输入条件上的无限循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54856793/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com