繁体   English   中英

错误:非法参数:未定义,MongoDB 中 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM