0

I found an API library for Woocommerce and trying to integrate it in Angular.

for fetching categories it looks like this line is needed.

const json = await WooWorker.getCategories();

My service looks like this:

 import { WooWorker } from "api-ecommerce";

  GetAllUsers(): Observable<any> {
    return this.http.get(`${BASEURL}/categories`);
  }

I need to replace this part return this.http.get(${BASEURL}/categories); with this const json = await WooWorker.getCategories(); since API uses the second way. How will be correct to do it with observable?

NewTech Lover
  • 1,185
  • 4
  • 20
  • 44
  • Looks like you want to [convert a Promise to an Observable](https://stackoverflow.com/q/39319279/9423231). Use `from(WooWorker.getCategories())` or `defer(() => WooWorker.getCategories())`. – frido Oct 13 '19 at 11:01

2 Answers2

0
GetAllUsers(): Observable<any> {
   return WooWorker.getCategories();
  }

Try this way as given above by returning the data from it

Hitech Hitesh
  • 1,641
  • 1
  • 9
  • 18
  • Thanks for replying. Will it work with https://github.com/inspireui/api-ecommerce/tree/master/src This kind of API? I am asking it because for API was mentioned react native but files look like simple JS withour react imports. – NewTech Lover Oct 11 '19 at 19:33
  • If it returning the json then you need to take the json by calling your observer function – Hitech Hitesh Oct 11 '19 at 19:36
0

You can write it in this way.

import { WooWorker } from "api-ecommerce";
import { Observable, of } from 'rxjs';

async GetAllUsers(): Observable<any> {
  const json = await WooWorker.getCategories();
  return of(json);
}

of is an RxJS operator that converts the input into an observable.

Ankush Jain
  • 5,654
  • 4
  • 32
  • 57