1. 20
    Query and Filter Documents from Firestore by userId with query()
    2m 51s

Query and Filter Documents from Firestore by userId with query()

Jorge Vergara
InstructorJorge Vergara
Share this video with your friends

Social Share Links

Send Tweet
Published a year ago
Updated a year ago

Now that tasks are actually associated with a user in our database, we want to only show a user their own tasks.

To do this we'll revisit our task list and make some changes to use the query() and where() methods to filter our list before we get it from the database.

We'll learn how to pass our user's ID to a query and fetch only the tasks that belong to that user. Because Firebase uses Observables, you'll also see how we handle and change observables in Angular.

Man 1: [0:01] Now that we started adding users to our tasks, we see that this task map out outflow has my user id here but this other task that I have here that says similar task for testing has a different user id. When I go into my application, I'm able to see both of them.

[0:22] Ideally, I will only be able to see my own tasks. For that we are going to go into our tasks page and we are going to check our tasks initialization. Here, I'm creating a reference to the tasks collection and then I'm using collection data to get the entire task list as an observable.

[0:46] We are going to bring AngularFire authentication here, and we are going to do a little more work in the task initialization. Here, we are going to start our observable stream by getting our user. We can get our user with the authState function that comes in AngularFire authentication.

[1:07] AuthState takes a parameter, the AngularFire authentication instance, and it returns an observable of the user. Since this is an observable, we can pipe into it. Here, we are going to get our user, and we are going to return a new observable. This is the collection data that we were returning before.

[1:34] Collection data takes a couple of different things. One can be the reference to the task that we are getting, but the other one can be a query. We are going to change this reference right here for a query. For that, we can use the query method from AngularFire. Our query is going to take two parameters.

[2:00] First, it's the same as we had before, it's the collection reference. Now that we have the collection reference, we can start passing our conditions. For conditions, AngularFire has a specific clause that's called where. This takes three parameters.

[2:19] The first parameter is the name of the property that we are going to evaluate against. In our case, it's user. The second parameter, it's going to be the operation that we are going to do. We are going to check for equality. The last parameter is the value against we are going to compare. In our case, it's going to be user UID. Now, when I save the application and reload, you see that now I only see my task.

egghead
egghead
~ just now

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today