Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type 'Promise<any>' is missing the following properties angular

I am making the http connection to my system in angular7. But, it is giving error in the module. This message appears when I created the get request:

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

Module:

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

Request:

users: User;

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

Http Get:

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

service:

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

like image 744
Leticia Fatima Avatar asked May 10 '19 20:05

Leticia Fatima


1 Answers

I found the problem, it was only to change the request of:

users: User;

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

for:

this.userService.getUsers().subscribe(
      response => {
        if (!response) {
          console.log(Error)
        } else {
          console.log(response)
          let users : User[] = response
        }
      })
like image 128
Leticia Fatima Avatar answered Nov 20 '22 01:11

Leticia Fatima