Auf Subcollection zugreifen

Manchmal möchte man in Firestore Untertabellen bzw. Untercollections verwenden, beispielsweise wenn man eine Collection invoices hat und die einzelnen Positionen der Rechnung in einer Subcollection mit dem Namen invoicePositions speichert. Glücklicherweise bietet Rowy diese Möglichkeit an:

Die Frage ist nun, wie man – beispielsweise aus einem Derivative-Field in der Hauptcollection – auf die Subcollection zugreift.

Hier ein Beispiel dazu:

`  // Get the reference to the document containing the subcollection
  const invoicePositionsRef = ref.collection('invoicePositions');

  // Get the snapshot, in this case sorted by the field "pos"
  const invoicePositionsSnapshot = await invoicePositionsRef.orderBy('pos').get();

  // Check if there are documents in the invoicePositions sub-collection
  if (invoicePositionsSnapshot.empty) {
    logging.error('No subcollection!');
    return;
  }

  // Iterate through the documents and do whatever
  // (in my case I build a html-invoice and put the lines together
  invoicePositionsSnapshot.forEach((doc) => {
    const data = doc.data();
    invoicePositionsHtml += data['htmlLine'];
  });`

Somit kann man einfach in Rowy auf Sub-Collections zugreifen!

1 Stern2 Sterne3 Sterne4 Sterne5 Sterne (1)

Loading…
Avatar von manuel

AUTOR

manuel