简体   繁体   English

Angular Reactive Forms 表单控件 valueChanges 停止侦听错误

[英]Angular Reactive Forms form control valueChanges stops listening on error

I have a registration form where I am using valueChanges listener on a form control and makes call to http service that posts to server if this value already exists, eg email and user name.我有一个注册表单,我在表单控件上使用valueChanges侦听器,并调用发送到服务器的 http 服务,如果该值已经存在,例如电子邮件和用户名。 If the value exists already on the server, then a status code 403 is returned.如果该值已存在于服务器上,则返回状态代码 403。 I am catching and handling this error by presenting a message to the user, but my problem is the valueChanges listener stops listening, so if I just change the form value, then this code is idle.我通过向用户显示一条消息来捕获并处理此错误,但我的问题是valueChanges侦听器停止侦听,因此如果我只是更改表单值,则此代码处于空闲状态。 Can I get the listener to continue even after the error?即使在错误发生后,我还能让侦听器继续吗? Should I be handling this another way?我应该以另一种方式处理吗? Thank you.谢谢你。

Here is listener code:这是侦听器代码:

    this.watchEmailSub = this.registerForm.controls.email.valueChanges.pipe(
      debounceTime(500),
      distinctUntilChanged(),
      switchMap(v =>
        this.regService.checkRegistrationValueExists('Email', v, this.registerForm.controls.email.valid)
      )
    ).subscribe(returnValue => {
        this.registerEmailError = null;
        console.log('returnValue.message = ', returnValue.message);
      },
      (error) => {
        console.warn('Error = ', error);
        this.registerEmailError = 'This email already exists';
      }
    );

HTTP service code: HTTP服务代码:

  checkRegistrationValueExists(type: string, value: string, formControlValid: boolean): Observable<any> {
    // Code removed here that defines checkValueExistsUrl & checkValueExistsBody
    if (formControlValid) {
      return this.http.post<string>(checkValueExistsUrl, checkValueExistsBody);
    }
    return of({ message: `${type} invalid` });
  }

You can use catchError and swallow the error to prevent this:您可以使用 catchError 并吞下错误来防止这种情况:

this.watchEmailSub = this.registerForm.controls.email.valueChanges.pipe(
  debounceTime(500),
  distinctUntilChanged(),
  switchMap(v =>
    this.regService.checkRegistrationValueExists('Email', v, this.registerForm.controls.email.valid).pipe(
      catchError(error => {
        this.registerEmailError = 'This email already exists';
        return EMPTY;
      })
    )
  )
).subscribe(returnValue => {
    this.registerEmailError = null;
    console.log('returnValue.message = ', returnValue.message);
  },
  (error) => {
    console.warn('shouldn't be here');
  }
);

IMO 403 shouldn't be thrown here. IMO 403 不应该被扔在这里。 403 is inapropriate in any event, as 403 means unauthorized to view this information, if any error, it'd be a 400. Beyond that though, this is an API endpoint specifically checking availability. 403 在任何情况下都是不合适的,因为 403 意味着未经授权可以查看此信息,如果有任何错误,它将是 400。不过,除此之外,这是一个专门检查可用性的 API 端点。 Error codes mean something went wrong.错误代码表示出现问题。 Nothing went wrong here, it checked and returned your answer successfully, yes it exists or no it doesn't.这里没有任何问题,它检查并成功返回了您的答案,是的,它存在或不存在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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