Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to query documents using document IDs in AngularFire?

I am trying to query documents based on document IDs but I am unable to get the data using

this.afs.collection<books>('books' , ref => ref.where( ref.id , 'in' , ['4L9uf4w5fqFXlU0uLbEM','bpXgEOmYqGor8uhUz2uq','tJFYPJcLMYNS8qnaUtMv'])).snapshotChanges().pipe(
            map(actions => {
            return actions.map( a => {
                const data = a.payload.doc.data();
                return {...data};
            });
        })
     );

afs is of type AngularFirestore

I don't know if above code is correct.

I tried the solution mentioned at : Query firestore database for document id by replacing ref.id with firebase.firestore.FieldPath.documentId() but I get an error :

'firebase' refers to a UMD global, but the current file is a module.

Help me in retrieving the data.

like image 681
sachin rathod Avatar asked Dec 31 '22 08:12

sachin rathod


1 Answers

I could solve it by replacing ref.id with firebase.firestore.FieldPath.documentId() and importing import * as firebase from 'firebase/app';

The issue can also be solved by importing import * as firebase from 'firebase'; but as mentioned by @Stratubas in the comments section above import gives

warning in the console

So, it is advised to use import * as firebase from 'firebase/app';

And finally my code looks:

import * as firebase from 'firebase/app';

this.afs.collection<books>('books' , ref => ref.where( firebase.firestore.FieldPath.documentId() , 'in' , ['4L9uf4w5fqFXlU0uLbEM','bpXgEOmYqGor8uhUz2uq','tJFYPJcLMYNS8qnaUtMv'])).snapshotChanges().pipe(
            map(actions => {
            return actions.map( a => {
                const data = a.payload.doc.data();
                return {...data};
            });
        })
     );
like image 66
sachin rathod Avatar answered Jan 07 '23 23:01

sachin rathod