12

I have a collection 'chats' with a members array which contains users participating in the chat. The problem is I want to get all chats where the current user is participating.

I do this with this query:

getUserChats(): Observable<Chat[]> {
    return this.auth.currUser
      .pipe(
        take(1),
        switchMap(user => this.afs
            .collection('chats', ref => ref.where('members', 'array-contains', `/users/${user.uid}`))
            .snapshotChanges()
            .pipe(
              map(actions => {
                return actions.map(action => {
                  const data = action.payload.doc.data() as Chat;
                  const id = action.payload.doc.id;
                  return {id, ...data};
                });
              })
            ) as Observable<Chat[]>
        )
      );
  }

This works well for strings, but doesn't work with References. How can I also make it work on References?

Image with Collections

The green works the red one doesn't work

Green: String Red: Reference

2 Answers 2

8

@Alex Mamo answer is right, you need a DocumentReference.

NOTE: YOU CAN'T CREATE ONE YOURSELF!

You have to get the reference by querying firebase!

Code:

return this.auth.currUser
      .pipe(
        take(1),
        switchMap(user => this.afs
            .collection('chats', ref => ref.where('members', 'array-contains', this.afs
              .collection('users')
              .doc<User>(user.uid)
              .ref))
            .snapshotChanges()
            .pipe(
              map(actions => {
                return actions.map(action => {
                  const data = action.payload.doc.data() as Chat;
                  const id = action.payload.doc.id;
                  return {id, ...data};
                });
              })
            ) as Observable<Chat[]>
        )
      );

The crucial part is the 'value' part which is:

this.afs
              .collection('users')
              .doc<User>(user.uid)
              .ref

You query and THEN get the reference with .ref!

That's it! That's how you query for a DocumentReference!

Sign up to request clarification or add additional context in comments.

Comments

5

It works with strings because you're passing as the third argument to the where() function the following argument:

/users/${user.uid}

Which is of type String. If you want to work with references as well, instead of a String, pass an actual DocumentReference object.

7 Comments

Do you know the Class Name? Can't find anything
And if I import it from angular/fire/firestore it's an interface
I don't understand, you cannot import the DocumentReference or you cannot pass the object?
Hi Filip! Is there everything alright, have you solved the issue?
|

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.