4

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.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
sachin rathod
  • 541
  • 1
  • 9
  • 23
  • Please don't show pictures of text on Stack Overflow. It's better to copy the text into the question itself so it's easier to read and search. – Doug Stevenson Jan 26 '20 at 16:15
  • From searching for this error message, it seems like you may be missing an import. https://stackoverflow.com/q/46092807, https://stackoverflow.com/q/46276558, and more – Frank van Puffelen Jan 26 '20 at 16:31
  • @FrankvanPuffelen I can't find any imports for the same in the package '@angular/fire/firestore' – sachin rathod Jan 26 '20 at 16:53
  • 1
    @sachinrathod I found this in one of my projects, give it a try: `import * as firebase from 'firebase/app';` – Stratubas Jan 26 '20 at 16:58
  • 1
    @Stratubas I referred this https://www.npmjs.com/package/angular-firebase and imported ```import * as firebase from 'firebase';``` and it worked and yes I tried import suggested by you it is also working. Thank You. I will write answer. – sachin rathod Jan 26 '20 at 17:08
  • 1
    @sachinrathod If I remember correctly, importing from `'firebase'` gives a warning in the console. Check it before answering. – Stratubas Jan 26 '20 at 17:09
  • 1
    @Stratubas yes you are right ```import * as firebase from 'firebase';``` gives warning. – sachin rathod Jan 26 '20 at 17:13

2 Answers2

9

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};
            });
        })
     );
sachin rathod
  • 541
  • 1
  • 9
  • 23
0

An alternative would be to add an id string property in your documents when creating them (make it equal to the actual id of the documents) and use that for querying.

Stratubas
  • 2,939
  • 1
  • 13
  • 18