typescript - 输入“承诺”' 缺少以下属性
问题描述
我正在 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();
});
}
路径 = 端点
解决方案
我发现了问题,只是更改了以下请求:
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
}
})
推荐阅读
- javascript - Thymeleaf JS 无法传递参数
- javascript - 重构javascript中的硬代码
- azure - IoT Workbench 中仅显示一个 Azure 订阅:Cloud Azure 配置命令
- angular - 如何对角度@Output 进行单元测试
- ios - Ionic Cordova Typescript iOS App - 自动从 BLE 检索数据
- css - 如何选择最后一个元素?
- javascript - 在 componentDidMount 或单击按钮时调用 API
- javascript - 在跨域 iframe 中获取 iframe id
- php - Symfony getData() 返回空值
- dns - 查询我的域名的名称服务器更改?