Search code examples
typescriptgoogle-cloud-firestoreloopback

Loopback 4 - Get subcollection from Firestore


Loopback 4 has a community maintained connector for Firestore here: https://github.com/dyaa/loopback-connector-firestore

How do I get the the subcollection houses from the following collection:

{
  village: {
    id: "yaOh(37Na",
    name: "Greenwich",
    houses: {
      house01: {
        address: "1st Street"
      },
      house02: {
        address: "2nd Street"
      }
    }
  }
}

The following snippet returns only shallow results:

const village = await this.villageRepository.findOne({where: {id: "yaOh(37Na"}});

console.log(village); // returns {id: "yaOh(37Na", name: "Greenwich"}


Solution

  • On Firestore reading from a collection, only reads the data from that collection and not from collections under it. There is no API that automatically reads data from subcollection too.

    You'll have to execute a separate read operation for each subcollection that you want to get data from.