【问题标题】:property map does not exist on type promise<void>类型 promise<void> 上不存在属性映射
【发布时间】:2019-07-23 04:23:17
【问题描述】:

在我的 Angular 6 项目中,我得到 property map does not exist on type promise 错误,即使我包含了

import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/toPromise';

以下是我的代码

 submit(): Promise<any> {
        return http.post('api/SubmitEmployee', JSON.stringify(this.formData))
            .map((response: Response) => <any>response.json())
            .toPromise()
            .catch(this.handlePromiseError)
    }

    handlePromiseError(error: Response) {
        console.log(error)
    }

经过一些谷歌搜索后,我找到了如下解决方案,但这也不起作用

import { map } from 'rxjs/operators';  

pipe(map((response: Response) => response.json()))

包含此内容后显示错误property type does not exist on type promise 以下是我自定义的post 方法

post(url: string, content: any, resolve: ResolveFunction = null, reject: RejectFunction = null) {
    this.clearCache();
    const retryFunc = _ => this.post(url, content, resolve, reject);
    return fetch(appSettings.baseApiUri + url, {
      method: 'POST',
      headers: this.getCustomHeader(),
      body: content
    })
      .then(result => {
        this.handleResponse(url, result, retryFunc, resolve, reject);
      })
      .catch(error =>
        this.debounce(_ => this.handleError(url, error, retryFunc).catch(_ => reject(error)), this.debounceTimeout)
      );
  }

【问题讨论】:

  • pipe 的变体只是使用了较新的运算符样式,请参阅:github.com/ReactiveX/rxjs/blob/master/doc/pipeable-operators.md。否则,如果 http.post 返回一个 Observable,我觉得它看起来不错,所以问题可能出在其他地方。
  • 如果您使用的是HttpClient,您根本不需要执行json(),因为这是自动完成的。

标签: angular rxjs angular6 rxjs6


【解决方案1】:

使用 from 像 from(http.post().then()) 一样包装 http.post,它返回一个 observable,以便您可以管道流

【讨论】:

    猜你喜欢
    • 2016-12-31
    • 2019-02-04
    • 2023-03-08
    • 2019-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-01
    • 2019-03-18
    相关资源
    最近更新 更多