【问题标题】:What does this error mean: 'Property 'then' does not exist on type '{ <T>(this: Observable<T>)'?这个错误是什么意思:'属性'then'在类型'{ <T>(this:Observable<T>)'上不存在?
【发布时间】:2019-06-28 23:46:55
【问题描述】:

我遇到了一个以前从未遇到过的奇怪错误,并且找不到相关的帖子。

错误是:

Property 'then' does not exist on type '{ <T>(this: Observable<T>): Promise<T>; <T>(this: Observable<T>, PromiseCtor: PromiseConstructor)...'.

我正在从使用.subscribe 切换到使用.toPromise().then()。我对其他几个 HTTP 请求做了同样的事情,没有问题。不知道这个有什么不同。

在服务中我有这个功能:

getData(user: string) {
    var url = some url
    let httpOptions = {
        headers: new HttpHeaders({
            some headers
          }),
          withCredentials: true };
    return this.http.get(url, httpOptions);
  }

然后在一个组件中我有

this._apiService.getData(lan).toPromise.then((information: any) =>
   {
        console.log(information)
   });

我在其他三个http 请求上做了一些非常相似的事情而没有遇到这个问题,并且服务调用的设置完全相同,组件功能也是如此。这个错误是什么意思?

【问题讨论】:

  • 不要忘记括号:.toPromise().then(...)
  • toPromise 是一个函数。
  • 简单的错字,现在可以关闭问题了。

标签: angular http promise


【解决方案1】:

你缺少括号()

toPromise().then( /*your code here*/)

这是一个例子https://codecraft.tv/courses/angular/http/http-with-promises/

【讨论】:

    猜你喜欢
    • 2020-03-29
    • 1970-01-01
    • 1970-01-01
    • 2021-08-26
    • 2022-06-17
    • 2021-11-16
    • 2020-11-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多