@ioclaudio wrote:
Hi,
I’d like to call a REST web service from an Ionic4 application and return an Observable with a list of User objects.
I’ve created a class User and I’d like that, during the cast, the constructor of this class is called.
I’ve tried these two methods:import {Observable} from 'rxjs'; import { map } from 'rxjs/operators'; class User implements IUser { id: number; email: string; firstName: string; lastName: string; city: string; search_string: string; ... constructor(values: Object = {}) { Object.assign(this, values); this.search_string = this.email + ' ' + this.firstName + ' ' + this.lastName; } } // Method-1 fetchUsers(): Observable<User[]> { console.log('*** fetchUsers ***'); ... return this.httpClient .get<User[]>(commandUrl, {headers: this.getWsHeader(), responseType: responseType}) .map(products => { return products.map((product) => new User(product)); }); } // Method-2 fetchUsers(): Observable<User[]> { console.log('*** fetchUsers ***'); ... return this.httpClient .get<User[]>(commandUrl, {headers: this.getWsHeader(), responseType: responseType}); }
The first has no errors but the constructor is not called during the cast.
The second gives the error:Property map does not exist on type Observable<User[]>
Which is the correct way to return a list of objects?
The class has the same fields of the Json plus the field:search_string
.Thank you very much
claudio
Posts: 1
Participants: 1