How to use "get" method to retrieve all collection in angularfire2
Asked Answered
C

6

6

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.

Confidence answered 29/11, 2017 at 9:15 Comment(2)
try .ref.get()Klayman
Awesome! I was looking for it. :) Thank you so much.Confidence
C
13

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());  
   })
})
Chavey answered 27/5, 2018 at 21:45 Comment(1)
Wish they made this more explicit. Not obvious from the start.Sha
N
5

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.

Nurserymaid answered 29/11, 2017 at 10:44 Comment(2)
Thanks, it works. Has there any way not to track the snapshot change event? I have seen it it the angularfire doc, but it seems to me that get() only provides the data of a specific time, and do not listen for any change. That is why looking for get(). Whether 'snapshotChanges' always trigger this of each change. I actually do not want to keep listening any event, Just need the data of that time.Confidence
Best option is to use first() or take(1).Nurserymaid
D
1

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

Defile answered 26/11, 2018 at 4:50 Comment(0)
T
1

Angular 8 with new @angularfire package

let query = this.fs.collection('users')

    return query.get()
    .pipe(
        map(snapshot => {
            let items = [];
            snapshot.docs.map(a => {
                const data = a.data();
                const id = a.id;
                items.push({ id, ...data })
            })
            return items
        }),
    )
Toots answered 2/12, 2019 at 19:52 Comment(0)
D
1

What I did when I wanted to retrieve a single document once from Firestore without the need of valueChanges or SnapshotChanges -- Angular Firestore

 this.db.collection<any>('members').doc(id)
.get()
.pipe(first())
.subscribe(res => {
   this.member = res.data()
 })
Designed answered 26/10, 2020 at 15:26 Comment(0)
P
0

My 2 cents are as follows, you could do something like this. In my use case I don't require to watch for value changes regularly, while a nice feature I don't want to be consistently making calls to the DB when data isn't changing frequently. So I've created a data service, which on app "start"/"init" I load the data needed up front (be sure to add some limits - in this case there is already limit on services), like this:

private getServices() {
  let data = [];
  const collection = this.afs.collection<Service>('services', ref => ref.where('isAddOn', '==', false).orderBy('name', 'asc'));
  collection.get().subscribe(snapshot => {
    snapshot.forEach((res) => {
      data.push(res.data());
    });
  });
  return this._services = data;
}

Then I can assign it to a getter and use it where necessary.

Keep in mind this works for my specific use case really well. As data isn't changing that often and I have a separate "refresh" call to manage updating data on the UI where necessary.

Hope this helps, any feedback and thoughts are also welcome.

Poult answered 3/9, 2021 at 0:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.