I'm having trouble converting an HTTP response into a DTO object. I can't seem to figure it out on my own and would appreciate some help with this issue.
I have a service that uses an adapter class to convert the response into an array of objects and then returns it to the controller. However, when trying to assign the HTTP service response to the controller variable, I receive an error: "error TS2339: Property 'collectionArray' does not exist on type 'Collection[]'".
DTO:
export class Collection {
private collectionId: number;
private image: string;
private url:string;
private title:string;
private description:string;
static getCollection(responseData) {
var obj = new Collection();
obj.setCollection_id(responseData.collection_id);
obj.setImage_url(responseData.image_url);
obj.setUrl(responseData.url);
obj.setTitle(responseData.title);
obj.setDescription(responseData.description);
return obj;
};
//getter setter
}
**Controller:**
import { Collection } from '../model/collection';
import {CollectionService} from './collection.service';
public collections : Collection[] = [];
//calling service
this.collectionService.getCollection().subscribe(
data => {
// giving error here
this.collections = data.collectionArray
},
err => {
console.log(err);
});
**service :**
getCollection() : Observable<Collection[]>{
// ...using get request
var options = new RequestOptions({
headers: new Headers({
'Accept': 'application/json',
'user-key' : '7d5ef14e15e096400df74871'
})
});
return this.http.get(this.url, options)
//returning arry of colletion object
.map((res:Response) => new CollectionAdapter(res.json()))
.catch((error:any) => Observable.throw(error.json().error || 'Server error'));
}
**Adapter:**
export class CollectionAdapter{
public collectionArray : Collection[] = [];
constructor(responseData){
this.getCollectionList(responseData.collections);
};
getCollectionList(response) {
for (var collectionObj of response) {
let collObj = collectionObj.collection;
let obj = Collection.getCollection(collObj);
this.collectionArray.push(obj)
}
return this.collectionArray;
};
}