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