Source:  Twitter logo

I a m trying to get the list of documents in collections, but m not getting any document, This is code what i m trying- `

    schoolProductCollectionFetch(schoolName){
    
       const school = this.db.collection(schoolName)
       this.school$ = school.snapshotChanges().pipe(map(schoollist=>schoollist.map(s=>{
         const data = s.payload.doc.id;
         console.log(data);
         return data;
       })));
       return this.school$
     
      }

Database Format: SchoolCollection->Documents->ProductCollection

SchoolCollection:{
   1st:{products:{}},
   2nd:{products:{}},
}

Have tried to represent the data format as in firestore, I want to get 1st, 2nd which are document id's for SchoolCollection, Schema Snap:

Note: Please let me know if more details needed to understand the query

You can try something like:

schoolProductCollectionFetch(): Observable<School[]> {
  return this.db
    .collection('schoolName')
    .snapshotChanges()
    .pipe(
      map((snaps) =>
        snaps.map((snap) => {
          return new School({
            id: snap.payload.doc.id,
            ...(snap.payload.doc.data() as {}),
          });
        }),
      ),
      first(),
    );
}

After you get the data and create an instance of it, you can refactor this code, to make it more readable. Hope this helps!

2 users liked answer #0dislike answer #02
Diego Herrera profile pic
Diego Herrera

First question I have: You are passing in a parameter 'schoolName' into the function then doing this.db.collection(schoolName). Now, is school name actually the name of the collection, or is it the ID of a document in the collection?

What error are you getting in your console output when you try this? Have you set up your firestore permissions to allow requests?

The database schema you posted is confusing, maybe an actual screen shot of your set up from the firebase console could be of more use.

EDIT

To get data for a collection using the firestore you have to build the correct path to the collection you want. In your example, doing something like this.db.collection(schoolName).get() will get all the documents in that collection. Note: THIS WILL NOT GET ANY SUB COLLECTIONS! This type of querying is not supported by Firebase at the time of writing this.

Let's say you want to get all the documents of the sub collection 'kitProducts'. This could be achieved by doing this.db.collection(schoolName + "/1st/kitProducts").get(). This returns all the documents in the sub collection 'kitProducts' that reside under a document with id '1st' in the schoolName collection. You must build these paths up in order to query them.

You seem to be working the realm of a real-time listeners on the collection. If this is your intention, you should use the onSnapshot() function. Taking the earlier example, writing something like this.db.collection(schoolName + "/1st/kitProducts").onSnapshot(). In this case, an event will be emitted any time a document within the kitProducts sub collection is edited, deleted, or added.

More reading:

https://firebase.google.com/docs/firestore/query-data/listen#listen_to_multiple_documents_in_a_collection

https://firebase.google.com/docs/firestore/query-data/get-data#get_all_documents_in_a_collection

1 users liked answer #1dislike answer #11
CuriousGeorge profile pic
CuriousGeorge

Not sure if you still need this but you may want to try collectionGroup('Name of collection').

Code should look like something similar to this:

    this.afs.collectionGroup('Users')
      .valueChanges().subscribe(users => {
      
      //this.userNames is an "Any" before my constructor.
      this.userNames = users

      //for loop allows me to get all the document's data
      for (let i = 0; i < users.length; i++) {
        
        //these const allow me to play with If conditionals
        const userEmail = this.userNames[i].email;
        const userRole = this.userNames[i].role;

        if (userRole == "User") {
          continue;
        } else {
          console.log(userEmail);
        }
      }
    });

HTH in anything...

1 users liked answer #2dislike answer #21
Jorge Alvarez profile pic
Jorge Alvarez

Copyright © 2022 QueryThreads

All content on Query Threads is licensed under the Creative Commons Attribution-ShareAlike 3.0 license (CC BY-SA 3.0).