Home > Mobile >  UNABLE TO Get multiple documents from a collection FROM FIRESTORE
UNABLE TO Get multiple documents from a collection FROM FIRESTORE

Time:09-21

I have coded the below snippet from firestore site and according to my database but the code isn`t working i have tried too but nothing else works

the WHERE condition is the main reason isn`t working else it works fine

 const test =() =>{
    try {
      dob.collection("users")
      .where("F_name", "==", true)
    .get()
    .then((querySnapshot) => {
        querySnapshot.forEach((doc) => {
            console.log(doc.id, " => ", doc.data());
        });
    })
    .catch((error) => {
        console.log("Error getting documents: ", error);
    });
    } catch{
      console.log("Erroror")
    }
  }
  

Database structure:

enter image description here

CodePudding user response:

enter image description here

This is the document image above @Dharmaraj

CodePudding user response:

As per the document data, the F_name field's value is a string but you are trying to find a boolean value with where("F_name", "==", true).

The data type must be same when using queries. Try:

dob.collection("users").where("F_name", "==", "asdf11")

Here the value is hard-coded but even if you want to pass user's input in the query, then you can pass a variable with string value.

CodePudding user response:

If you want to fetch all users from the database you can just do a get without any query. But it's probably not what you want.

dob.collection("users").get()

If you are looking for several user name values you can use the in operator (https://firebase.google.com/docs/firestore/query-data/queries#in_not-in_and_array-contains-any)

dob.collection.where('F_name', 'in', ['asdf11', 'asdf123']).get();

If you are looking for users that has a provided a value for the F_name field, then you can use the != operator: https://firebase.google.com/docs/firestore/query-data/queries#not_equal_

// If you store it as null when name is not set
dob.collection.where('F_name', '!=', null).get();

// If you store it as empty string when name is not set
dob.collection.where('F_name', '!=', '').get();
  • Related