Angular firestore query to firestore document
Asked Answered
C

4

6

I have below query

    selectedUser$: AngularFirestoreDocument<any>;
    this.selectedUser$ = this.userCollection.ref.where('uid', '==', key)

Throwing error

Type 'Query' is not assignable to type 'AngularFirestoreDocument'. Property 'ref' is missing in type 'Query'.

I tried

this.selectedUser$ = this.userCollection.ref.where('uid', '==', key).get()

no success

Basically, I want the query to return firestore document

Contrabandist answered 29/8, 2018 at 6:55 Comment(2)
Tried this.selectedUser$ = this.userCollection('yourCollection', ref => ref.where('uid', '==', key) ) ? This will retrieve the document on valueChanges().subscribe()Carnassial
yourCollection??? userCollection is given thereContrabandist
C
17

The error your getting is because you're mixing the firebase native api & angularfire.

selectedUser$: AngularFirestoreDocument<any>;

Calling .ref on your AngularFirestoreCollection converts it to type firebase.firestore.CollectionReference

That being said, there are 2 possibilities to solve your issue:

Using angularfire:

I'm assuming your userCollection looks something like this: this.afs.collection<User>. Since you're querying a collection, there is no way to ensure your query predicate uid == key is unique in firebase. So you query the collection and limit() the result. This will return you an array with one document. flatMap()will return you a single user.

this.afs.collection<User>('users', ref => ref.where('uid', '==', key).limit(1))
   .valueChanges()
   .pipe(
       flatMap(users=> users)
   );

Using the firebase native api:

const query = this.usersCollection.ref.where('uid', '==', key);
query.get().then(querySnapshot => {
    if (querySnapshot.empty) {
        console.log('no data found');
    } else if (querySnapshot.size > 1) {
        console.log('no unique data');
    } else {
        querySnapshot.forEach(documentSnapshot => {
            this.selectedUser$ = this.afs.doc(documentSnapshot.ref);
            // this.afs.doc(documentSnapshot.ref).valueChanges().subscribe(console.log);
            });
        }
    });

This way it's a bit easier to chain multiple .where clauses if it's needed

Convolve answered 29/8, 2018 at 8:29 Comment(0)
J
2

This worked for me with the latest versions: 2021-July

this.store
    .collection("Products",ref=>ref.where("stock","==",10))
    .get()
    .subscribe(data=>data.forEach(el=>console.log(el.data())));

PS: I'm using get() and no event listener.

Jeaz answered 14/7, 2021 at 8:32 Comment(0)
S
0
this.afs.collection<User>('users', ref => ref.where('uid', '==', key).limit(1))
      );
Spheroidal answered 18/6, 2022 at 7:55 Comment(1)
While this code may solve the question, including an explanation of how and why this solves the problem would really help to improve the quality of your post. Remember that you are answering the question for readers in the future, not just the person asking now. Please edit your answer to add explanations and give an indication of what limitations and assumptions apply.Contempt
A
-3

It will be easier to help you if you add more details about the structure of your Firestore collection and your typescript file.

However, to query the collection I do the following:

  1. Define a private AngularFirestore in the constructor.

    constructor(
      private afStore: AngularFirestore,
    ) 
    {
    }
    
  2. Define the query and pass the result to the AngularFirestoreDocument.

    this.selectedUser$ = this.afStore
        .collection('TheNameOfYourCollection').doc<any>(key);
    
Andryc answered 29/8, 2018 at 7:20 Comment(1)
Here we need to pass the id generated by firestore only, I need to query on a fieldContrabandist

© 2022 - 2024 — McMap. All rights reserved.