【问题标题】:TypeError: this.networkService.getRequest(...).pipe is not a function in ionic AngularTypeError: this.networkService.getRequest(...).pipe 不是 ionic Angular 中的函数
【发布时间】:2021-01-20 11:31:12
【问题描述】:

单击选项卡栏中的第二个选项卡会显示此错误:

core.js:6241 ERROR Error: Uncaught (in promise): TypeError: this.networkService.getRequest(...).pipe is not a function
TypeError: this.networkService.getRequest(...).pipe is not a function
at PaymentService.getPaymentList (payment.service.ts:359)
at PaymentComponent.loadDataPaymentState (payment.component.ts:188)
at PaymentComponent.ngAfterViewInit (payment.component.ts:130)

这些选项卡中的两个页面正在使用 PaymentComponent。一切都在第一次打开的选项卡中工作。单击第二个选项卡会导致错误。

发生错误的代码是:

getpaymentList() {
let list = this.buildListURL();
return this.networkService.getRequest(list).pipe(
  tap(
    (data) => {
      console.log(data);
   
      this.lastbill= data["bill"];
    },
    (error) => {
      console.log(" error");
      console.log(error);
    }
  )
);

}

getRequest的代码:

getRequest(path, customHeader?) {
let options = this.createOptions();
if (customHeader) {
  options.headers = this.jsonConcat(options.headers, customHeader);
}

if (!this.isOnline) {
  this.errorOccurred(null);
}
let cachedData = this.cacheService.getCachedNode(path);
if (cachedData) {
  return cachedData;
} else {
  return this.http
    .get(
      path,
      options
    )
    .pipe(
      map(
        (data) => {
          console.log("data");
          return data;
        },
        (error) => {
          console.log("error");
          console.log(error);
          this.errorOccurred(error);
        }
      )
    );
}

}

我无法弄清楚问题所在。如果有人对此有任何想法,请告诉我。谢谢

【问题讨论】:

  • getRequest(path, customHeader?) 更改为 getRequest(path, customHeader?): Observable<YourType> 并告诉我们它是否可以解决您的管道问题,但会破坏您的 getRequest 方法。
  • @Silvermind 在将 getRequest(path, customHeader?) 更改为 getRequest(path, customHeader?) 后给出相同的错误:Observable
  • 您的输入可能有误。尝试禁用缓存系统并始终返回 http 逻辑。如果这样可以解决问题,您应该检查您的缓存机制。
  • @Silvermind 你是对的,禁用缓存后缓存机制有问题
  • 听起来 cachedData 无法通过管道输出。你可以尝试用 RxJS 运算符包装它吗:return of(cachedData) ?

标签: angular ionic-framework rxjs ionic4 rxjs-observables


【解决方案1】:

您的代码给出错误,因为调用方法需要一个可观察的,当您第一次调用 getRequest 方法时缓存中没有数据并且 else 子句 中的数据被调用但是下次调用此函数时,缓存中有数据,if (cachedData) 子句被调用但

if (cachedData) {
   return cachedData;
}  

没有返回 observable。

如果您将对其进行小的更改,那么它也可以与缓存代码一起使用。你必须做以下事情:

  1. 第一:

    import { of } from "rxjs";
    
  2. getRequest 方法进行一些更改,而不是

    if (cachedData) {
       return cachedData;
    }   
    

   if (cachedData) {
       return of(cachedData);
   } 

【讨论】:

    猜你喜欢
    • 2022-12-05
    • 1970-01-01
    • 1970-01-01
    • 2018-07-08
    • 2017-07-17
    • 2018-06-10
    • 1970-01-01
    • 2018-11-30
    相关资源
    最近更新 更多