gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-30 21:56:03 25 4
gpt4 key购买 nike

我正在使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com