skip to Main Content

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

3

Answers


  1. Chosen as BEST ANSWER

    I have edited and works fine, but i want to add that list of email_id in the dropdown but doesn`t works

      var email_user=[];
      const [open, setOpen] = useState(false);
      const [value, setValue] = useState(null);
      const [items, setItems] = useState(email_user);
    
    try {
          dob.collection("users")
          .where('email_id', '!=', "")
          .get()    
        .then((querySnapshot) => {
            querySnapshot.forEach((doc, i) => {
                // console.log(doc.id, " => ", doc.data());
                console.log(doc.data().email_id)
                for (let i = 0; i < doc.data().length; i++) {
                  email_user[i] = doc.data().email_id[i];
                  console.log("email_user : ", email_user[i])
                }
            });
        })
        .catch((error) => {
            console.log("Error getting documents: ", error);
        });
        } catch{
          console.log("Error")
        }
       
          
    
    
    
           <DropDownPicker
            open={open}
            value={value}
            items={items}
            setOpen={setOpen}
            setValue={setValue}
            setItems={setItems}
          />
    

    enter image description here


  2. 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.

    Login or Signup to reply.
  3. 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();
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search