0

I am new to react native firebase firestore. I am designing an app where the functionality is that the user should be able to search for a registered user on any of the three criteria:

-by Name

-by Expertise

-by Location

If the user explicitly types in these fields, my code will retrieve the filtered result from firestore

Code in react native to retrieve from firestore:

var fbdbStartRef= firebase.firestore().db.collection("users");
    var queryResult = routeRef
.where(("Name", "==", NameInput)
.where("Expertise", "==", ExpertiseInput)
.where("Location","==" , LocationInput))
.get().then(function(snapshot){/* ... */}

Scenario: If the user did not type in any search criteria in the UI for any one field, say "Location" , in that scenario I dont want to set the filter for that criteria from firestore. That means , the expected code should be:

componentDidMount() {

const fbdbStartRef=firebase.firestore().collection("users");
const nameFilter=NamesearchInput ? (fbdbStartRef.where("firstName","==",NamesearchInput)):fbdbStartRef;
const expertiseFilter= ExpertiseSearchInput ? (nameFilter.where("Expertise","==",ExpertiseSearchInput)):nameFilter;
const locationFilter=LocationSearchInput ? (expertiseFilter.where( "Location","==",LocationSearchInput)): expertiseFilter;

console.log("location filter", locationFilter)
   locationFilter.get().then((querySnapshot) => {
      const mentorlistLastnameFromDB = [];
      querySnapshot.forEach((doc) => {
        mentorlistLastnameFromDB.push({
          first: doc.data().firstName,
          last: doc.data().lastName,
          location: doc.data().Location,
          expertise: doc.data().Expertise,
          profilepic: doc.data().profilePicUrl,
          userID:doc.data().userID,
        });
        console.log("mentor list from db 0", mentorlistLastnameFromDB)
      });
      this.setState({
        mentorListAll: mentorlistLastnameFromDB,
      });
      console.log("mentor list", mentorlistLastnameFromDB)
      });
}

render() {
    return (
            <FlatList
              data={this.state.mentorListAll}

              renderItem={
                ({ item, index }) => {
                  return (

                    <Text { documentid:item.userID}/ >
/>
                  



 

Question: However the query did not get the result. Per the console.log the query result array is empty Please refer the attached screen shot for the VSS code . Can you please review and suggest what am I doing wrong hereenter image description here

The below mentioned threads did not answer my query. Hence opening a new question How to query data from Firestore with dynamic input [duplicate] Can you create dynamic firebase queries? How to build a Firestore query chain based on a dynamic response from a promise

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
Bhol
  • 145
  • 3
  • 16
  • are you using composite indexes? or getting any error? also, is it possible to print the result from the filter and confirm if the search criteria terms are actually valid for the DB – alan Aug 11 '20 at 21:43
  • @alan thank you for the analysis/questions. I found the issue is due to the initial value set at " " with space which is not retrieving the result from db. – Bhol Aug 11 '20 at 22:00

0 Answers0