【发布时间】: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