加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angular – TypeError:您在预期的流中提供了“undefined”

发布时间:2020-12-17 18:00:11 所属栏目:安全 来源:网络整理
导读:我有一个Ionic应用程序,其用户提供程序具有signup()方法: doSignup() { // set login to same as email this.account.login = this.account.email; // Attempt to login in through our User service this.user.signup(this.account).subscribe((resp) = {
我有一个Ionic应用程序,其用户提供程序具有signup()方法:

doSignup() {
  // set login to same as email
  this.account.login = this.account.email;
  // Attempt to login in through our User service
  this.user.signup(this.account).subscribe((resp) => {
    this.navCtrl.push(MainPage);
  },(err) => {
    //console.log('error in signup',err);
    // ^^ results in 'You provided 'undefined' where a stream was expected'
    //this.navCtrl.push(MainPage);

    // Unable to sign up
    let toast = this.toastCtrl.create({
      message: this.signupErrorString,duration: 3000,position: 'top'
    });
    toast.present();
  });
}

出于某种原因,此代码从不调用成功回调,只调用错误处理程序.如果是这样,它会导致您在上面的评论中看到的错误.

我的user.signup()方法如下所示:

signup(accountInfo: any) {
  return this.api.post('register',accountInfo).share();
}

我的Api类看起来如下:

import { HttpClient,HttpParams } from '@angular/common/http';
import { Injectable } from '@angular/core';

/**
 * Api is a generic REST Api handler. Set your API url first.
 */
@Injectable()
export class Api {
  public static API_URL: string = 'http://localhost:8080/api';

  constructor(public http: HttpClient) {
  }

  get(endpoint: string,params?: any,reqOpts?: any) {
    if (!reqOpts) {
      reqOpts = {
        params: new HttpParams()
      };
    }

    // Support easy query params for GET requests
    if (params) {
      reqOpts.params = new HttpParams();
      for (let k in params) {
        reqOpts.params.set(k,params[k]);
      }
    }

    return this.http.get(Api.API_URL + '/' + endpoint,reqOpts);
  }

  post(endpoint: string,body: any,reqOpts?: any) {
    return this.http.post(Api.API_URL + '/' + endpoint,body,reqOpts);
  }

  put(endpoint: string,reqOpts?: any) {
    return this.http.put(Api.API_URL + '/' + endpoint,reqOpts);
  }

  delete(endpoint: string,reqOpts?: any) {
    return this.http.delete(Api.API_URL + '/' + endpoint,reqOpts);
  }

  patch(endpoint: string,reqOpts);
  }
}

我尝试从user.signup()中删除share(),并返回Observable< any>,但这没有帮助.

解决方法

我在使用generator-jhipster-ionic(yo jhipster-ionic give v3.1.2)创建一个新项目时遇到了这个问题,遵循 Matt Raible(OP) Use Ionic for JHipster to Create Mobile Apps with OIDC Authentication博客文章,但选择了JWT身份验证而不是OIDC.

问题的根源是各种各样的问题.

我在使用livereload服务器运行Ionic应用程序时出现问题,CORS问题正在发生,Angular HTTP返回(未知URL)的经典HTTP失败响应:0 Unknown Error,其中0是HTTP错误代码.
但是,在当前情况下,Observable级别的错误处理会隐藏该问题.

当您遵循Angular’s HttpClient #Error Details部分建议,并在HTTP POST调用上添加带有catchError运算符的Observable管道时,您可以获得正确的HTTP错误详细信息.在这种情况下,而不是下到rxjs / util / subscribeToResult.js:71(TS source #L80)TypeError:你提供了’undefined’,其中一个流是预期的默认错误情况,它转到rx / util / subscribeToResult.js:23 (TS source #L34),并在管道方法中正确处理错误.

在遵循Observable调用之后,我发现当前的默认身份验证拦截器,如此模板src/providers/auth/auth-interceptor.ts中所示,捕获HTTP错误401并且对其他人没有任何作用,基本上将它们静音并阻止它们的传播.

TL; DR在JWT的情况下,解决方案是简单地删除src / providers / auth / auth-interceptor.ts .catch(…)块,允许错误传播到login.service.ts,并在其中. authServerProvider.login(credentials).subscribe((data)=> {…},(错误)=> {…})错误回调.

我相信问题和解决方案对于您的OIDC案例,其注册方法和错误回调可能是相同的.

[编辑]甚至更多,因为相同的.catch代码可以在第一篇帖子评论中提到的入门示例中找到:ionic-jhipster-starter – auth-interceptor.ts#L31

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读