Type 'Promise' 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...
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
2 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
}
})
It's hard to tell from your code, How I usually perfrom request like that is with HttpClient using generic get
public get(id: number): Observable<className> {
return this._http.get<className>(`${this._baseUrl}/${id}`);
}
and then use like this
this.service.get(id).subscribe(x => this.myClass = x);
but you are using service, not HttpClient, and you do not specify your return...
Does your getUsers() function returns 1 or manu users?
if you return a list, you should simply change your request variable to users: User[];