I am attempting to write an Angular 6 service that connects to the DreamFactory API.
I have managed to extract json data from the "brand" table in a database that I have allowed DreamFactory to manage. I am able to send a get request to the API so there is no problems with comms. Below is a sample:
{
"resource": [
{
"id": 1,
"name": "Apple",
"display_order": 1,
"visible": true
},
{
"id": 2,
"name": "Dell",
"display_order": 6,
"visible": true
},
{
"id": 3,
"name": "HP",
"display_order": 5,
"visible": true
},
{
"id": 4,
"name": "HTC",
"display_order": 4,
"visible": true
},
{
"id": 5,
"name": "Samsung",
"display_order": 2,
"visible": true
},
{
"id": 6,
"name": "Sony",
"display_order": 3,
"visible": true
}
]
}
I am struggling with the formatting of the data:
How do I map the fields in the "resource" response to a class I created in Angular? I have imported “brand.ts” in to “data.service.ts”. I have imported HTTPClient in all the correct places like “app.module.ts” and in “data.service.ts” and used it in constructor() {} etc.
'../models/brand.ts'
export class Brand{
id: number;
name: string;
displayOrder: number;
isVisible: boolean;
}
‘../services/data.service.ts’
...
getBrands() {
this.http.get(this.baseUrl + 'brand' + '?api_key=' + this.APP_API_KEY)
.map((data: Response) => {
return data;
})
.subscribe((data => {
console.log(data);
this.brands = data;
})
);
}
...
By using the above code in the console log my data outputs like the following:
resource: (6) […]
0: Object { id: 1, name: "Apple", display_order: 1, … }
1: Object { id: 2, name: "Dell", display_order: 6, … }
2: Object { id: 3, name: "HP", display_order: 5, … }
3: Object { id: 4, name: "HTC", display_order: 4, … }
4: Object { id: 5, name: "Samsung", display_order: 2, … }
5: Object { id: 6, name: "Sony", display_order: 3, … }
length: 6 …
How do I modify my code to map the data to my class? For example the field “display_order” needs to map to my Angular class property “displayOrder” etc. Any help would be appreciated as I am a noob to Angular.