【问题标题】:Function is not checking the condition before navigating to the route in Angular在导航到 Angular 中的路线之前,函数没有检查条件
【发布时间】:2019-01-20 09:48:11
【问题描述】:

在后端的用户模式中,我们在用户模型中有 isVerified。我们将 Mongoose 与 Node.js 一起使用。我们尝试检查 isVerified 是否为假,然后它应该导航到验证路由,但它似乎导航到当结果为真时它应该获得的路由。在数据库中它是错误的。这是代码:

 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']);
        }
      }
    );
  }
}

为什么它不从模型中检查 isVerified 是真还是假?

【问题讨论】:

  • 您是否尝试过在 loginUser subscribe 函数中输出数据?我假设它会在这里返回登录失败的原因。此外,您只在 getUserById 函数中设置了 isVerified - 您没有使用该函数
  • @wodka 你能解释一下上面的代码示例吗?我试图从 Getuserbyid 获取经过验证的值。

标签: angular typescript angular6


【解决方案1】:

如果 http 请求成功并且如果 http 请求不成功,您的代码将发送给人们路由,然后它会检查 isVerfied 是否为假,然后去验证路由。
如果您的 isVerify 字段来自 db,那么它会显示 go to succeed 功能。

【讨论】:

  • isVerified in the Db is false.
【解决方案2】:

您的代码中有几个问题

// 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']);
        }
      }
    );
  }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-07
    • 2019-03-01
    • 1970-01-01
    • 2021-04-21
    • 1970-01-01
    • 2020-08-16
    • 2020-01-04
    • 2022-07-25
    相关资源
    最近更新 更多