Angularjs   发布时间:2022-04-20  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了angular – TypeError:您在预期的流中提供了“undefined”大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个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

大佬总结

以上是大佬教程为你收集整理的angular – TypeError:您在预期的流中提供了“undefined”全部内容,希望文章能够帮你解决angular – TypeError:您在预期的流中提供了“undefined”所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。