Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use "get" method to retrieve all collection in angularfire2

Firebase cloud store provides "get" method to retrieve the entire collection as following -

    db.collection("users").get().then((querySnapshot) => {
        querySnapshot.forEach((doc) => {
            console.log(doc);
        });
    });

I am using Angularfire2 version 5.0.0rc3 in my ionic 3 project to connect with firebase cloud storage.

I am trying to access this get method as following -

    constructor(
        private afs: AngularFirestore
    ) {
    
        this.afs.collection("users").get().then((querySnapshot) => {
            querySnapshot.forEach((doc) => {
                console.log(doc);
            });
        });
            
    }

But here, "get" method is not working. Can anyone tell me who to use this 'get' method with firebase clould storage and angularfire2.

like image 548
Tahmina Khatoon Avatar asked Nov 29 '17 09:11

Tahmina Khatoon


People also ask

How do I get all the collections in firestore?

js admin clients have listCollections() on Firestore to get that list. Or, if you're looking for subcollections nested under a document, use DocumentReference. listCollections(). If you want to get a list on any platform, you should maintain that list yourself in a known collection inside a known document id.

Can you have a collection in a collection firestore?

A collection contains documents and nothing else. It can't directly contain raw fields with values, and it can't contain other collections. (See Hierarchical Data for an explanation of how to structure more complex data in Cloud Firestore.)


3 Answers

Actually, it works, just call firestore this.afs.firestore.collection

let userDoc = this.afs.firestore.collection(`users`);
userDoc.get().then((querySnapshot) => { 
   querySnapshot.forEach((doc) => {
        console.log(doc.id, "=>", doc.data());  
   })
})
like image 110
quince Avatar answered Nov 06 '22 19:11

quince


Not exist get() method in the AngularFirestore collection, use subscribe instead.

Here is an example:

this.afs.collection("users").snapshotChanges().map(actions => {
  return actions.map(a => {
    const data = a.payload.doc.data();
    const id = a.payload.doc.id;
    return { id, ...data };
  });
}).subscribe((querySnapshot) => {
    querySnapshot.forEach((doc) => {
        console.log(doc);
    });
});

I recommend you to read angularfire2 guide first.

like image 25
coturiv Avatar answered Nov 06 '22 19:11

coturiv


I am also facing same issue. for me the browser hangs when large data is getting retrieved. Moreover valueChanges() listens for new changes and the piece of code starts execution again.

For retrieving large data, its better to write a cloud function. Get the data from the cloud function and then display that under Angular application.

Firebase Cloud Functions

like image 30
Nikhil Avatar answered Nov 06 '22 19:11

Nikhil