Saya mencoba membuat formulir login untuk situs web saya. Saya menggunakan agular , express dan mongoDB

Inilah pengontrol fungsi login saya:

loginUser: (req, res) => {
    User.findOne({
            username: req.body.login_username
        })
        .then(user => {
            bcrypt.compare(req.body.login_password, user.password)
                .then(result => {
                    if (result) {
                        req.body.login_username = user.username
                        res.json({
                            message: "Success!",
                            added: true
                        });
                    } else {
                        console.log('Failed login attempt')
                        res.json({
                            message: "Error!",
                            error: err
                        });
                    }
                }).catch(console.error, console.log(req.body.login_password, user.password))
        })
}

Dan inilah komponen login saya:

  userID: string;
  userData: any;
  loginUser: any;
  error = "";

  constructor(
    private router: Router,
    private route: ActivatedRoute,
    private httpService: HttpService
  ) {}

  ngOnInit() {
    this.userID = this.route.snapshot.paramMap.get("id");
    this.getSingleUser();
    this.loginUser = { username: "", password: "" };
  }

  getSingleUser() {
    let observable = this.httpService.getOneUser(this.userID);
    observable.subscribe((data) => {
      this.userData = data;
    });
  }
  onSubmit() {
    let observable = this.httpService.loginUser(this.loginUser);
    observable.subscribe((data: any) => {
      console.log("Wrong");
      if (data.error) {
        this.error = data.error.errors.name.message;
      } else {
        this.getSingleUser();
        this.router.navigate([""]);
      }
    });
  }

Ketika saya mengklik tombol terminal saya mendapatkan kesalahan seperti ini:

undefined $2b$20$8DmOjsDm3h5q/jEq9lNauezUdFYdL6EBt9gjmCu8/0DU0kAnSSIA2 Kesalahan: data dan argumen hash diperlukan di Object.compare (/Users/nhannguyen/Desktop/personal_project/instagram/node_ben) /Desktop/personal_project/instagram/node_modules/bcrypt/promises.js:29:12 di Janji baru () di Object.module.exports.promise (/Users/nhannguyen/Desktop/personal_project/instagram/node_modules/bcrypt/promises.js :20:12) di Object.compare (/Users/nhannguyen/Desktop/personal_project/instagram/node_modules/bcrypt/bcrypt.js:204:25) di /Users/nhannguyen/Desktop/personal_project/instagram/server/controllers/users .js:65:24 di processTicksAndRejections (internal/process/task_queues.js:97:5)

1
nathannewyen 28 Mei 2020, 11:06

1 menjawab

Jawaban Terbaik

Harap tambahkan .catch(console.error) setelah .then() untuk menangkap kesalahan penuh.

Setelah itu periksa apakah nilainya benar-benar ada dan jika tidak, periksa apakah plugin body-parser sudah diinstal

0
DutchKevv 28 Mei 2020, 08:35