首页 > 解决方案 > 输入“承诺”' 缺少以下属性

问题描述

我正在 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();
        });
    }

路径 = 端点

标签: typescripthttpangular7

解决方案


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

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
        }
      })

推荐阅读