1

Here is the User collection within my firestore database:

enter image description here

I am trying to use the below method to only return Users where isMechanic === false:

private _mechanics = new BehaviorSubject<User[]>([]);

  get mechanics() {
    return this._mechanics.asObservable();
  }

 return of(
      firebase.firestore().collection("users").where("isMechanic", "==", "false")
        .get()
        .then((docs) => {
          const data = []
          docs.forEach((doc) => {
            data.push(doc);
          });
          this._mechanics.next(data)
          console.log('Mechanics in UsersService:', this._mechanics);
          console.log('Mechanics in UsersService:', this.mechanics);
        }).catch((err) => {
          console.log(err);
        })
    );

No records are currently beingWhen this method is called, an empty array is logged to the console. So no records are being returned even though (as per screenshot) isMechanic is false for this record.

Can someone please tell me what changes are required for this function to work as expected?

2
  • first of all, where is isMechanic in users? Commented May 3, 2020 at 11:23
  • It's not working because isMechanic isn't a property on the objects in the array. Have you tried looking in the nested properties to see where the property is? For example, in metadata. I don't use Firestore, but I imagine this should be documented in the official documentation? Commented May 3, 2020 at 11:36

2 Answers 2

1

I managed to figure this out just now.

If I replace "false" with false, then the users collection is filtered as expected.

Replacing

firebase.firestore().collection("users").where("isMechanic", "==", "false")

with

firebase.firestore().collection("users").where("isMechanic", "==", false)

returns only users where isMechanic == false

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

Comments

0

LoadedMechanics is an empty array:

Then surely user.isMechanic is either undefined or null

Look at the following example

let data = [
{
},
{
   IsMechanic: undefined
},
{
   IsMechanic: null
},
{
   IsMechanic: false
}
]

console.log(data)

data = data.filter(pr => pr.IsMechanic === false)

console.log(data)

filter function ignored most the cases.

Now the question is, what is users object? It looks like an array of documents for me. Usually to get all properties from document you have to use ...data() function.

users.map(user => ({
  id: user.id,
  ...user.data()
}))

did you do that in your this.usersService.users observable?

--Edit

You could change following code

const data = []
docs.forEach((doc) => {
data.push(doc);
this._mechanics.next(data);
});

to

const data = docs.docs.map(doc => ({id: doc.id, ...doc.data()}));
this._mechanics.next(data);

Also according to docs

You should filter boolean properties using literal values, not double quote them

.where("isMechanic", "==", "false")

.where("isMechanic", "==", false)

2 Comments

Thanks for your answer. I've updated my question above as I'm going to try to filter the data when querying it in firebase rather than loading all users initially when there's no need
Updated an answer. get() function in this case returns a query object which contains props like empty, docs. You get map all documents into new array of objects with .map(doc => ({id: doc.id, ...doc.data()}));

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.