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.

7
  • 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. Commented Jan 26, 2020 at 16:15
  • 1
    @sachinrathod I found this in one of my projects, give it a try: import * as firebase from 'firebase/app'; Commented Jan 26, 2020 at 16:58
  • 1
    @Stratubas I referred this 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. Commented Jan 26, 2020 at 17:08
  • 1
    @sachinrathod If I remember correctly, importing from 'firebase' gives a warning in the console. Check it before answering. Commented Jan 26, 2020 at 17:09
  • 1
    @Stratubas yes you are right import * as firebase from 'firebase'; gives warning. Commented Jan 26, 2020 at 17:13

2 Answers 2

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};
            });
        })
     );
Sign up to request clarification or add additional context in comments.

2 Comments

This code helps solve queries that have security rules, +1 mate!
@KingDarBoja only when you know document ID ;)
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.

1 Comment

Is it possible to retrieve data without adding id string property to document?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.