Ionic/Angular: How to merge data from Firebase?

Viewed 37

I need help merging data from Firebase in an Ionic Angular project. I have a collection of stock data and one for my labels. I need to merge the two. After a number of tries, I used components to get the job done, but the search function will only search one set of data. Meaning that it will search for the label ID, that I store in the Stock data, but not the actual current title for that label. So I want to merge the data, so that my search function will search all the data. Or at least the Stock data and the label titles.

Here is the model of my StockData.

export interface StockData {
  id?: string;
  title: string;
  date: string;
  labels: string[];
  amount: string;
}

Each Stock take can have many labels (or none).

export class Label {
  id?: string;
  title: string;
  changes: string[];
}

Currently, I use a service to get the data from Firebase. Here is my code for a stock take:

getStockData(): Observable<StockData[]>{
    const labelRef = collection(this.firestore, `stockData`);
    return collectionData(labelRef, {idField: 'id'}) as Observable<StockData[]>;
}

In my page ts file, I then get the data like this.

stockData: StockData[] = [];

async loadStockData() {
    await this.countService.getStockData().subscribe(res => {
      this.stockData = res;
    });
}

Then in my HTML, I use a component to display the labels. This is done by passing the id as an input to the component. (However, as mentioned, they are not searched.) Again I use a service to get my label data:

getLabels(): Observable<Label[]>{
    const labelRef = collection(this.firestore, `labels`);
    return collectionData(labelRef, {idField: 'id'}) as Observable<Label[]>;
}

I would appreciate any pointers on how to merge the data. I also want to bring in other data like users, which like labels will be in a separate collection from the stock take. However, I first want to fix this issue before I decide on a way forward. Thank you.

0 Answers
Related