angular - TypeError:无法读取未定义 Angular 的属性“forEach”
问题描述
ObtenerClientes(cantidad: number): Observable<Array<Cliente>> {
const urlCliente = `${this.url}/?results=${cantidad}`;
const lstClientes: Array<Cliente> = [];
return this.http.get<any>(urlCliente).pipe(
map(clientes => {
(clientes.results as Array<any>).forEach((registro: any ) => {
lstClientes.push({
id: registro.CustomerID,
nombre: registro.ContactName,
companyName: registro.CompanyName
});
});
return lstClientes;
})
);
}
}
拜托,我需要一些帮助!
解决方案
您应该尝试map
在数组上使用该方法。
在这里,试一试:
ObtenerClientes(cantidad: number): Observable < Array < Cliente >> {
const urlCliente = `${this.url}/?results=${cantidad}`;
const lstClientes: Array < Cliente > = [];
return this.http.get < any > (urlCliente).pipe(
map(clientes => (clientes.results as Array < any > ).map((registro: any) => ({
id: registro.CustomerID,
nombre: registro.ContactName,
companyName: registro.CompanyName
})))
);
}
推荐阅读
- mysql - 1台服务器,每个子域使用不同的数据库
- javascript - fullcalendar V3 如何添加 JET 上下文菜单
- javascript - mdast-util-to-markdown:防止列表项之间发生换行符?
- ada - Ada 和 GNATStudio 的路径问题
- javascript - IndexOf 方法未按预期工作 JAVASCRIPT
- python - 使用请求在 Python 中填写多步表单
- typescript - 打字稿:为什么 keyof Union 永远不会
- node.js - 将多个 Angular SSR 应用程序部署到 Digital Ocean 液滴的最佳方式是什么?
- java - 无效指令“targetApi”,有效指令为,Remove,replace,Stricttt
- php - PHP laravel前端如何上传/发送csv文件到后端api?