I am making an E-Commerce Website using MEAN Stack and Im facing problems with bcrypt. Im not able to create a new user.
Backend - 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);
});
users-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>
users-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!'
});
}
);
}
Please help me solve this
The onSubmit() method didnt have password field previously. Ricardo Machado was right about password not existing. I checked it in the backend, but failed to notice it in angular frontend.
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);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.