typescript - 类型 'Promise<any>' 缺少以下属性

标签 typescript http angular7

我正在使用 angular7 与我的系统建立 http 连接。但是,它在模块中给出了错误。当我创建 get 请求时出现此消息:

Type 'Promise<any>' is missing the following properties from type 'User': id, user, email

模块:

export class User { 
  id: number
  user: string
  email: string
}

要求:

users: User;

    this.userService.getUsers().subscribe(
          response => {
            if (!response) {
              console.log(Error)
            } else {
              this.users = response
            }
          })

HTTP 获取:

getUsers() {
        return this.service.get(this.endpoint)
        .map((response) => {
            return response;
        });
    }

服务:

standardHeaders() {
        const headers = new HttpHeaders();
        headers.append('Content-Type', 'application/json');
        if (this.authToken) {
            headers.append('X-Authorization', this.authToken);
        }
        return { headers: headers };
    }

 get(path: string) {
        return this.http.get(this.url + path, this.standardHeaders()).map((response: Response) => {
            return response.json();
        });
    }

path = endpoint

最佳答案

我发现了问题,只是更改了请求:

users: User;

this.userService.getUsers().subscribe(
          response => {
            if (!response) {
              console.log(Error)
            } else {
              this.users = response
            }
          })

对于:

this.userService.getUsers().subscribe(
      response => {
        if (!response) {
          console.log(Error)
        } else {
          console.log(response)
          let users : User[] = response
        }
      })

关于typescript - 类型 'Promise<any>' 缺少以下属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56084233/

相关文章:

angular - 如何使用 typescript 在angular2中获取设备显示的高度和宽度?

angular - 如何通过 Angular 4 中的共享模块正确导入 Angular Material 模块?

rest - 在Golang中使HTTP请求非法路径

http - 最终获得 POST 的表单上的键值对是什么?

c++ - 使用 winsock 库发送 GET http 请求时出现 400 bad request 错误

angular 7 + angular cli 7+ ng 测试不工作

angular - Sass 伪选择器在 Angular 中不起作用

javascript - matDatepicker 在选择日期前 1 天发送请求

javascript - 服务 promise 返回处理过的数据以实现组件的另一个 promise

Angular Material Vertical Stepper 单一形式 .reset() 无法正常运行