Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript RxJS throwError事件未定向到处理程序?_Javascript_Node.js_Angular_Typescript_Rxjs - Fatal编程技术网

Javascript RxJS throwError事件未定向到处理程序?

Javascript RxJS throwError事件未定向到处理程序?,javascript,node.js,angular,typescript,rxjs,Javascript,Node.js,Angular,Typescript,Rxjs,尝试和IIUC登录的第二个参数应接收throwError的结果,但事实并非如此。任何想法: login(username: string, password:string) { this.authenticate(username, password).subscribe( user => { this.ostore.put(USER_KEY, user); this.ostore.put(AUTHENTICATION_ERROR_K

尝试和IIUC登录
的第二个参数应接收
throwError
的结果,但事实并非如此。任何想法:

  login(username: string, password:string) {
    this.authenticate(username, password).subscribe(
      user => {
        this.ostore.put(USER_KEY, user);
        this.ostore.put(AUTHENTICATION_ERROR_KEY, false);
        this.router.navigate(['/']);
      }),
      (error)=>{
        console.log("Storing the Error");
        error => this.ostore.put(AUTHENTICATION_ERROR_KEY, AUTHENTICATION_ERROR_MESSAGE);
      }
  }


  private authenticate(username:string, password:string) {
  // Mock Authentication Check
  if (username !== 'user') {
      return throwError(AUTHENTICATION_ERROR_MESSAGE);
  }
  return of({ name: username });
  }

接收错误的不是第二个参数,而是SUBSCRIBE的第二个回调。您的“登录”方式如下:

login(username: string, password:string) {
  this.authenticate(username, password).subscribe(
    user => {
      this.ostore.put(USER_KEY, user);
      this.ostore.put(AUTHENTICATION_ERROR_KEY, false);
      this.router.navigate(['/']);
    },
    error => {
      console.log("Storing the Error");
      error => this.ostore.put(AUTHENTICATION_ERROR_KEY, AUTHENTICATION_ERROR_MESSAGE);
    })
}

请看这里:

太棒了-刚刚意识到我把第一个括号放错地方了-谢谢@奥立:哈哈,是的,这是常有的事!