我在注册表单时遇到问题,当我单击提交且所有字段均已填写且一封电子邮件(已在DB中)重置表单后,第二次单击错误消息(DB中已有电子邮件,其他味精...)出现..什么问题?
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('username', 'Veuillez renseigner un identifiant').notEmpty();
req.checkBody('email', 'Veuillez renseigner une adresse email valide').isEmail();
req.checkBody('password', 'Veuillez renseigner un mot de passe').notEmpty();
req.checkBody('password2', 'Les mots de passe ne correspondent pas').equals(req.body.password);
var errors = req.validationErrors();
if(errors) {
res.render('register', {errors:errors});
} else {
User.findOne({ email: email }).then(user => {
if (user) {
req.flash('error_msg','EMAIL ALREADY IN DB');
res.render('register', {errors:errors});
} else {
var newUser = new User({
email: email,
username: username,
password: password
});
User.createUser(newUser, (err,user) => {
if(err) throw err;
console.log(user)
})
req.flash('success_msg','u can now log u account is created')
res.redirect('/users/login')
}
});
}
});
感谢您的帮助:)
答案 0 :(得分:1)
似乎您不等待createUser回调函数在响应之前就响应 验证一切成功。
答案 1 :(得分:0)
如GisliBG所示,您必须等待CreateUser回调;这意味着,您需要将需要在createUser完成其工作之后执行的代码放在回调中:这是尊重异步代码的一般规则/行为:
尝试一下:
msSqlCurrencyPushService
答案 2 :(得分:0)
更新!它的工作方式是这样,但是当我输入重复的电子邮件但没有密码时,只有电子邮件错误味精,除此以外所有工作正常
这是我的注册文件
{{#if errors}}
{{#each errors}}
<div class="alert alert-danger">{{msg}}</div>
{{/each}}
{{/if}}
{{#if error}}
{{#each error}}
<div class="alert alert-danger"></div>
{{/each}}
{{/if}}
<h2 class="page-header">Register</h2><br>
<form action="/users/register" method="post">
<div class="form-group">
<label for="username">Username</label>
<input type="text" class="form-control" placeholder="Username" name="username">
</label>
</div>
<div class="form-group">
<label for="email">Email</label>
<input type="text" class="form-control" placeholder="Email" name="email">
</label>
</div>
<div class="form-group">
<label for="password">Password</label>
<input type="password" class="form-control" placeholder="Password" name="password">
</label>
</div>
<div class="form-group">
<label for="password2">Confirm Password</label>
<input type="password" class="form-control" placeholder="Password" name="password2">
</label>
</div>
<button class="btn btn-primary" type="submit">Submit</button>
</form>
和我的users.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('username', 'Veuillez renseigner un identifiant').notEmpty();
req.checkBody('email', 'Veuillez renseigner une adresse email valide').isEmail();
req.checkBody('password', 'Veuillez renseigner un mot de passe').notEmpty();
req.checkBody('password2', 'Les mots de passe ne correspondent pas').equals(req.body.password);
var errors = req.validationErrors();
var error = 'email not available'
if(errors) {
res.render('register', {errors: errors})
} else {
User.findOne({ email: email })
.then(user => {
if(user) {
//USER EXIST
res.render('register', {errors: errors, error: error})
} else {
var newUser = new User({
email: email,
username: username,
password: password
});
User.createUser(newUser, (err,user) => {
if(err) throw err;
console.log(user);
req.flash('success_msg','u can now log u account is created');
res.redirect('/users/login');
})
}
})
}
});