首页 > 解决方案 > 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;       
      })    
    );    
  }   
}

拜托,我需要一些帮助!

标签: angularforeach

解决方案


您应该尝试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
    })))
  );
}

推荐阅读