类型“ Observable <未知>”缺少类型“ Promise <any>”中的以下属性

时间:2019-08-30 14:30:16

标签: angular

我正在将角度应用程序升级到角度8,并收到以下错误 我已更改的是使诺言可传递

     Type 'Observable<unknown>' is missing the following properties
    from type 'Promise<any>': then, catch, [Symbol.toStringTag]
return this.http.post(this._checkExecuteTradeUrl, requestBody, this.getRequestHeaders())
       .pipe(toPromise());

我当前的代码是

import { map, tap, toPromise } from 'rxjs/operators';

public CheckExecuteTrade(model: TradeNotification): Promise<any> {

        const header = new HttpHeaders({ 'Content-Type': 'application/json' });

        const requestBody = JSON.stringify(model);
        return this.http.post(this._checkExecuteTradeUrl, requestBody, this.getRequestHeaders())
            .pipe(toPromise());
    }

1 个答案:

答案 0 :(得分:1)

toPromise()不是documentation中的可管道运算符,因为它:

  

不是可插值运算符,因为它不返回可观察值

toPromise()移到pipe()之外:

return this.http.post(this._checkExecuteTradeUrl, requestBody, this.getRequestHeaders())
  .toPromise();

如果您需要执行其他可管道运算符,您仍然可以在pipe()中使用它们,但是不能在其中添加toPromise()

return this.http.post(this._checkExecuteTradeUrl, requestBody, this.getRequestHeaders())
  .pipe(map(r => r.toLowerCase(), tap(r => console.log(r))
  .toPromise();

希望有帮助!