I have got 3 services with similar methods that return data from database. I am using them in my component and want to store each data respond in corresponding component array. How do i get the rest of data from other services? I am not really familiar with forkjoin, etc. Would be nice if you give an example.
Services:
export class BrandService {
private url: string;
constructor(private http: HttpClient) {
this.url = 'http://localhost:3000/brands';
}
public getJsonBrands(): Observable<any> {
return this.http.get(this.url);
}
}
__________________________________________
export class ProductService {
private url: string;
constructor(private http: HttpClient) {
this.url = 'http://localhost:3000/products';
}
public getJsonProducts(): Observable<any> {
return this.http.get(this.url);
}
}
__________________________________________
export class CategoryService {
private url: string;
constructor(private http: HttpClient) {
this.url = 'http://localhost:3000/categories';
}
public getJsonCategories(): Observable<any> {
return this.http.get(this.url);
}
}
Component:
export class ProductsComponent implements OnInit {
productList: Array<any>;
brandList: Array<any>;
categoryList: Array<any>;
constructor(private productService: ProductService, private brandService: BrandService, private categoryService: CategoryService) {
this.productService.getJsonProducts().subscribe(
product => {
this.productList = product;
})
ngOnInit() {
}
}
_______________________________________
Code below doesnt seem to work, i keep getting console error "this.brandService.getJsonBrands is not a function"
ngOnInit() {
forkJoin(
this.productService.getJsonProducts(),
this.brandService.getJsonBrands(),
this.categoryService.getJsonCategories()
)
.subscribe(([res1, res2, res3]) => {
this.productList = res1;
this.brandList = res2;
this.productList = res3;
console.log(this.productList, this.brandList, this.productList);
},
error1 => {
console.log(error1);
});
}