简体   繁体   中英

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)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM