Angular TypeError:无法读取属性';forEach&x27;未定义角的 obtenerclients(cantidad:number):可观察{ 常量urlCliente=`${this.url}/?results=${cantidad}`; 常量lstClientes:Array=[]; 返回此.http.get(urlCliente.pipe( 地图(客户=>{ (clientes.results作为数组).forEach((registro:any)=>{ lstClientes.push({ id:registro.CustomerID, 名称:registro.ContactName, 公司名称:registro.companyName }); }); 返回客户; }) ); } }

Angular TypeError:无法读取属性';forEach&x27;未定义角的 obtenerclients(cantidad:number):可观察{ 常量urlCliente=`${this.url}/?results=${cantidad}`; 常量lstClientes:Array=[]; 返回此.http.get(urlCliente.pipe( 地图(客户=>{ (clientes.results作为数组).forEach((registro:any)=>{ lstClientes.push({ id:registro.CustomerID, 名称:registro.ContactName, 公司名称:registro.companyName }); }); 返回客户; }) ); } },angular,foreach,Angular,Foreach,拜托,我需要一些帮助 您应该尝试在数组上使用map方法 在这里,尝试一下: ObtenerClientes(cantidad: number): Observable<Array<Cliente>> { const urlCliente = `${this.url}/?results=${cantidad}`; const lstClientes: Array<Cliente> = []; ret

拜托,我需要一些帮助

您应该尝试在数组上使用
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>).forEach((registro: any ) => {       
          lstClientes.push({   
            id: registro.CustomerID,   
            nombre: registro.ContactName,     
            companyName: registro.CompanyName      
          });           
        });        
        return lstClientes;       
      })    
    );    
  }   
}
obtenerclients(cantidad:number):可观察>{
常量urlCliente=`${this.url}/?results=${cantidad}`;
常量lstClientes:Array=[];
返回this.http.get(urlCliente).pipe(
map(clientes=>(clientes.results作为数组).map((registro:any)=>({
id:registro.CustomerID,
名称:registro.ContactName,
公司名称:registro.companyName
})))
);
}

这意味着它不是数组,因此没有forEach方法
clients.results
未定义的
,因此您的查询不会返回任何内容。
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
    })))
  );
}