【问题标题】:Property 'subscribe' does not exist on type 'OperatorFunction<Response, {}>''OperatorFunction<Response, {}>' 类型上不存在属性 'subscribe'
【发布时间】:2019-10-21 11:12:23
【问题描述】:

我知道这已经被问过很多次了,但是,我无法弄清楚如何使用以下帖子更改我自己的代码。

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);
  }

我使用官方文档https://ionicframework.com/docs/native/http 和本教程创建了这个请求

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);
    });
}

错误信息:

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

从我的阅读来看,这是 Promises 和 rxjs 之间的冲突。

问题: 我需要更改什么才能使其正常工作?有没有办法在我的代码中添加所需的“管道”函数以使其工作?

【问题讨论】:

  • tagRequest 函数在哪里?还有为什么你只在 specificObjectRequest 中使用 return ?
  • 我添加了 tagRequest

标签: javascript angular typescript ionic-framework


【解决方案1】:

在这里使用promises似乎没有必要,你可以更轻松地做到这一点

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);
    });
}

【讨论】:

  • requestMapTags前面不需要加async吗?
  • 不,没有必要,因为你在这里不使用 Promises,因为 async/await 仅用于 Promises
  • 您的代码有效,唯一的问题是我得到的是 res 是一个字符串,但我希望它是 json 格式,有什么办法可以在代码中解析它吗? (无论如何谢谢:))
  • 你的意思是说字符串,字符串里面有对象吗?还是只是字符串值?,res 是什么意思?,res 在tagRequest 或requestMapTags 内?,实际上后端应该以正确的格式发送它
  • 后端以正确的“json”格式发送它,由于某种原因,当它到达所需的函数时,res 是字符串格式,我现在通过添加 JSON.parse(水库)
【解决方案2】:

您的tagRequest() 需要有一个返回值作为Observable 而不是OperatorFunction&lt;Response, {}&gt;;

所以试试这样的:

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, {}, {}));
  }

【讨论】:

  • 我使用了您的代码和 Artyom Amiryans 代码的组合,它有效,谢谢!同样的问题,有什么办法可以将响应解析为json?因为我得到一个字符串格式的资源
  • 当然可以使用res.json()获取JSON版本
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-06-06
  • 1970-01-01
  • 2017-10-20
  • 1970-01-01
  • 1970-01-01
  • 2016-10-31
  • 2016-09-09
相关资源
最近更新 更多