handsome
handsome

Reputation: 2392

get collection of documents with firestore

I was following this example:

var docRef = db.collection("cities").doc("SF");

docRef.get().then(function(doc) {
    if (doc.exists) {
        console.log("Document data:", doc.data());
        // this is not working
        const people = doc.collection("people");
    } else {
        // doc.data() will be undefined in this case
        console.log("No such document!");
    }
}).catch(function(error) {
    console.log("Error getting document:", error);
});

I want to return an array of people within the city of SF.

According to https://firebase.google.com/docs/database/web/structure-data

when you fetch data at a location in your database, you also retrieve all of its child nodes.

but I can´t find the right to return the collection within a doc (without making a new query)

Upvotes: 1

Views: 107

Answers (1)

Peter Haddad
Peter Haddad

Reputation: 80904

In Firestore when you retrieve the data, you retrieve all the fields inside a document. If you want to retrieve data from another document or from subcollection then you need to do a different query.

Upvotes: 1

Related Questions