简体   繁体   中英

Property 'subscribe' does not exist on type 'OperatorFunction<Response, {}>'

Im aware that this has been asked multiple times before, however, I could not figure out how to change my own code using following posts.

Property 'subscribe' does not exist on type 'OperatorFunction<Response, Recipe[]>'

Property 'subscribe' does not exist on type 'OperatorFunction<Product[], ParamMap>'

Property 'subscribe' does not exist on type 'OperatorFunction<unknown, any>'


rest-api.service.ts

specificObjectRequest(ids: string[]): Observable<any> {
  var idString = ids.join(); 
  const url = `${ApiServerUrl}/media_objects?ID=${idString}`;
  console.log("requesting from url: " + url);
  this.http.get(url, {}, {})
    .then(res => this.extractData = res.data)
    .catch(this.handleError);
  return;
}

tagRequest(topic: string){
    var url = `${ApiServerUrl}/tags?topic=${topic}`;
    url = encodeURI(url);
    console.log("requesting from url: " + url);
    this.http.get(url, {}, {})
      .then(res => this.extractData = res.data)
      .catch(this.handleError);
    return map(this.extractData);
  }

I created this request using the official documentation https://ionicframework.com/docs/native/http and this tutorial

filter.page.ts

async requestMapTags(topic: string){
    await this.api.tagRequest(topic).subscribe(res => {  //Property 'subscribe' does not exist on type 'OperatorFunction<Response, {}>'
      console.log(res);
      this.tagsRequestAnswer = res; 
    }, err => {
      console.log(err);
    });
}

Error Message:

[ng] ERROR in src/app/filter/filter.page.ts(89,35): error TS2339: Property 'subscribe' does not exist on type 'OperatorFunction<Response, {}>'.

From what I have read, it's a conflict between Promises and rxjs.

Question: What do I need to change to get this working? Is there a way to add the required 'pipe' function in my code in order for it to work?

Your tagRequest() need to have a return value as an Observable not OperatorFunction<Response, {}> ;

So try something like this:

import { from } from 'rxjs';

tagRequest(topic: string){
    var url = `${ApiServerUrl}/tags?topic=${topic}`;
    url = encodeURI(url);
    console.log("requesting from url: " + url);
    return from(this.http.get(url, {}, {}));
  }

using promises here seems unnecessary, you can do it without more easily

import { of } from 'rxjs';

tagRequest(topic: string){
    var url = `${ApiServerUrl}/tags?topic=${topic}`;
    url = encodeURI(url);
    return of(this.http.get(url, {}, {})).pipe(map(res => res.data));
  }

requestMapTags(topic: string){
    this.api.tagRequest(topic).subscribe(res => { 
      console.log(res);
      this.tagsRequestAnswer = res; 
    }, err => {
      console.log(err);
    });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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