Home > Software design >  Observable error while trying to return function result
Observable error while trying to return function result

Time:01-02

I need to get results from api and subscribe them to variable. My code is

getInnerData = (obj: CampaignLineItem) => {
  let packetLineItemsParams = graphqlHelper.getWhereParams();
  packetLineItemsParams.where._and.push({
    packet: { _eq: obj.Packet },
  });
  let tempObj: any = { ...obj };
  this._campaignLineItemService
    .getLineItemsByPacket(packetLineItemsParams)
    .subscribe((data) => {
      tempObj.innerDataItems = data.data;
      obj = tempObj;
      return obj;
    });
};

public mapObjects = (items: CampaignLineItem[]): Observable<CampaignLineItem[]> => {
  let usersData: CampaignLineItem[] = items.map((obj) => ({
    ...obj,
    innerDataItems: this.getInnerData(obj),
  }));
  return usersData;
};

this.mapObjects(
  items
).subscribe((data) => {
  this.dataSource.data = data;
});

I am getting the following error on return enter image description here Any ideas why

CodePudding user response:

The return type of mapObjects is Observable<CampaignLineItem[]>. That is not CampaignLineItem[], which is what usersData is.

I think this is what you can do to get past this error:

import { of } from 'rxjs';
...
return of(usersData);
  • Related