![](/img/trans.png)
[英]keeps getting "Illegal arguments: undefined, string at Object.bcrypt.hashSync"
[英]Error: Illegal arguments: undefined, string at Object.bcrypt.hashSync in MongoDB
我正在使用 MEAN Stack 制作電子商務網站,但我面臨 bcrypt 的問題。 我無法創建新用戶。
后端 - users.js:
router.post('/', async (req, res) => {
let user = new User({
name: req.body.name,
email: req.body.email,
passwordHash: bcrypt.hashSync(req.body.password, 10),
phone: req.body.phone,
isAdmin: req.body.isAdmin,
street: req.body.street,
apartment: req.body.apartment,
zip: req.body.zip,
city: req.body.city,
country: req.body.country
});
user = await user.save();
if (!user) return res.status(400).send('the user cannot be created!');
res.send(user);
});
用戶-form.component.html:
<div class="p-field p-col-4">
<label for="password">Password</label>
<input formControlName="password" id="password" type="password" pInputText />
<small *ngIf="userForm.password.invalid && isSubmitted" class="p-error"
>Password is required</small
>
</div>
用戶-form.component.ts
private _addUser(user: User) {
this.usersService.createUser(user).subscribe(
(user: User) => {
this.messageService.add({
severity: 'success',
summary: 'Success',
detail: `User ${user.name} is created!`
});
timer(2000)
.toPromise()
.then(() => {
this.location.back();
});
},
() => {
this.messageService.add({
severity: 'error',
summary: 'Error',
detail: 'User is not created!'
});
}
);
}
請幫我解決這個問題
onSubmit() 方法以前沒有密碼字段。 Ricardo Machado 關於密碼不存在是正確的。 我在后端檢查了它,但在角度前端沒有注意到它。
onSubmit() {
this.isSubmitted = true;
if (this.form.invalid) {
return;
}
const user: User = {
id: this.currentUserId,
name: this.userForm.name.value,
password: this.userForm.password.value,
email: this.userForm.email.value,
phone: this.userForm.phone.value,
isAdmin: this.userForm.isAdmin.value,
street: this.userForm.street.value,
apartment: this.userForm.apartment.value,
zip: this.userForm.zip.value,
city: this.userForm.city.value,
country: this.userForm.country.value
};
if (this.editmode) {
this._updateUser(user);
} else {
this._addUser(user);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.