简体   繁体   中英

Function is not checking the condition before navigating to the route in Angular

In the user schema in the backend, we have isVerified in the user model. We use Mongoose with Node.js. We try to check if isVerified is false then it should navigate to the verify route but it seems it navigates to the route where it should get when the result is true. While in the DB it's false. here is the code:

 SigninForm: FormGroup;
  forbiddenEmails: any;
  faTimes = faTimes;
  errorMessage: string;
  user: any;
  isVerified: any;

  constructor(
    private authService: AuthService,
    private router: Router,
    private usersService: UsersService,
    private tokenService: TokenService) {

  }


  ngOnInit() {
    this.SigninForm = new FormGroup({
      'username': new FormControl(null, [Validators.required, Validators.minLength(4)]),
      'password': new FormControl(null, [Validators.required,
      Validators.minLength(4)
      ]),
    });
  }

  getUserById(user) {
    this.usersService.GetUserById(user._id).subscribe(
      data => {
        this.user = data.result;
        this.isVerified = data.result.isVerified;
      },
    );
  }
  signinUser() {

    this.authService.loginUser(this.SigninForm.value).subscribe(
      data => {
        this.tokenService.SetToken(data.token);
        this.SigninForm.reset();
        setTimeout(() => {
          this.router.navigate(['people']);
        }, 3000);
      },
      err => {

        if (err.error.message) {
          this.errorMessage = err.error.message;
        }
        if (this.isVerified === false) {
          this.router.navigate(['verify']);
        }
      }
    );
  }
}

Why it doesn't check isVerified is true or false from the model?

Your code is sending to people route if http request succeed and if http request not success and then it checks if isVerfied is false then go to verify route.
If your isVerify fields is coming from db then it show go to succeed function.

You have several Problems in your code

// never Called!
  getUserById(user) {
    this.usersService.GetUserById(user._id).subscribe(
      data => {
        this.user = data.result;
        this.isVerified = data.result.isVerified;
      },
    );
  }
  signinUser() {

    this.authService.loginUser(this.SigninForm.value).subscribe(
      data => {
// might also return in case the loginUser fails (and it still returns data) -> try to output the data to see what is happening in this case
        this.tokenService.SetToken(data.token);
        this.SigninForm.reset();
        setTimeout(() => {
          this.router.navigate(['people']);
        }, 3000);
      },
      err => {

        if (err.error.message) {
          this.errorMessage = err.error.message;
        }
// it will never be false since it is either undefined or true
        if (this.isVerified === false) {
          this.router.navigate(['verify']);
        }
      }
    );
  }

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.

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