I want to query firebase like so but it doesn't allow me. Is there a way to dynamically query firebase? I need to handle very specific cases - for example if the user selects "Doesn't Matter" then I need to remove that preference filter from the query, some of the fields are array datatypes and some are strings and some are numbers. I need to be able to handle all forms of datatypes and populate the where clause appropriately. Manually checking each field and calling out for each field will be get taxing on the system (there are over 20 fields to query on). Any advise would be great!
for(ddp in docDataPreferences){
if(docDataPreferences[ddp] == "Doesn't Matter"){
allDoesntMatterPreferences.push(ddp);
} else if(docDataPreferences[ddp] != "Doesn't Matter" && docDataPreferences[ddp] != '' && docDataPreferences[ddp] != undefined) {
keysWithValues.push(ddp);
whereClause += ".where('preferences."+ ddp + "', 'array-contains-any', " + JSON.stringify(docDataPreferences[ddp]) +")"
}
}
var usersMatchesCollection = config.db.collection("Users");
var query = usersMatchesCollection + whereClause;
await query.get().then( function (matchesQuerySnapshot) {
matchesQuerySnapshot.forEach(function(doc) {
//...do something
})
})
TypeError: query.get is not a function. (In 'query.get()', 'query.get' is undefined)]
I can see that the where clause is printing out correctly but I assume you can't concat the object and the string. When I manually add the returned whereClause like so:
var query = usersMatchesCollection.where('preferences.datingPrefDistance', '==', 22).where('preferences.datingPrefDrinking', '==', "No").where('preferences.datingPrefEducation', '==', "Doctorate").where('preferences.datingPrefKids', '==', "No").where('preferences.datingPrefMarried', '==', "No").where('preferences.datingPrefSmokingCig', '==', "No").where('preferences.datingPrefSmokingPot', '==', "No").where('preferences.prefDrinking', '==', "No").where('preferences.prefEducation', '==', "Doctorate").where('preferences.prefIdentifyAs', '==', "Male").where('preferences.prefKids', '==', "No").where('preferences.prefMarried', '==', "No").where('preferences.prefSmokingPot', '==', "No")
it works. So it is the concatenation that is causing an issue.
Is there a work around?