0

I have two collections: "vehicles" and "clients". In each vehicle, I'm registering the client via client's document Id. I need to access the client's title using the key I'm storing in the vehicle document and display it in the Material DataTable that shows a list of vehicles. However, I can't seem to be able to make it work and I'm pretty boggled with this.

The image below displays the table where I need to display the client title using another document. Rest of the values are being pulled from the vehicle's own document.

enter image description here

So far, I had unsuccessful attempts with switchMap and with flatMap (possibly due to lack of experience in using these operators). The only successful attempt was obtaining the client title via a nested observable query from the "vehicles" observable and it performs extremely bad. (Client titles disappear after sorting the table)

This is how I'm fetching the vehicles observable:

    this.vehicles$ = this.afs.collection<IVehicle>(`vehicles`);
    this.vehicles = this.vehicles$.snapshotChanges().pipe(
  map(actions => {
    return actions.map(a => {
      const data = a.payload.doc.data() as IVehicle;
      const $key = a.payload.doc.id;
      return { $key, ...data };
    });
  })
);

This is how I was able to fetch the titles (malfunctions in sort table event - titles disappear for some reason)

    this.vehicles = this.vehicles$.snapshotChanges().pipe(
  map(actions => {
    return actions.map(a => {
      const data = a.payload.doc.data() as IVehicle;
      const $key = a.payload.doc.id;
      const client = data.client;
      const clientTitle = this.afs
        .doc(`clients/${client}`)
        .snapshotChanges()
        .pipe(
          map(foo => {
            const dataFoo = foo.payload.data() as IClient;
            let titleString = dataFoo.title;
            if (client) {
              return { titleString, ...dataFoo };
            } else {
              titleString = 'N/A';
              return { titleString, ...dataFoo };
            }
          })
        );
      return { clientTitle, $key, ...data };
    });
  })
);

I must be doing something wrong while trying to use switchMap, as I can't seem to get the document Id from the vehicle document, even though the vehicle interface includes a "client" field. (see the image below) That's why I didn't complete that snippet to return granular values to append onto the "vehicle" observable.

enter image description here

I just can't seem to catch what am I doing wrong, I would appreciate any direction that shows a "correct" way to approach these types of queries.

ReturnTable
  • 355
  • 4
  • 12
  • 1
    I think this could help you: https://stackoverflow.com/a/57962973/6294072 – AT82 Oct 01 '19 at 06:42
  • @AJT82 I don't seem to be able to access the client property of the vehicle document in order to make queries when using switchMap. I can, however, access the vehicle document Id but for this instance, that'd be pretty irrelevant. Was it supposed to be this hard to query another document? I have to be doing something wrong somewhere... – ReturnTable Oct 01 '19 at 15:12
  • @AJT82 Thank you so very much! :) Looked again to that answer and used mergeMap instead of switchMap and voila! `this.vehicles = this.afs.collection('vehicles').snapshotChanges().pipe( mergeMap(actions => combineLatest( actions.map(foo => {.....` Your timing was great! If you want to post that as an answer please do. – ReturnTable Oct 01 '19 at 16:04
  • 1
    Great! Glad I was at the right place at right time :P – AT82 Oct 01 '19 at 16:06
  • @AJT82 I do have one question though, how would you handle if you wanted to merge another document as well? – ReturnTable Oct 01 '19 at 18:52

0 Answers0