I have a function that accepts 2 parameters and both can be empty. when I was getting errors, I found that we can add "if"
my query changed from this (of course before introducing groceryType)
const querySnapshot = await store.collection("groceries")
.where("title", ">=", searchString)
.where("title", "<", searchString+"\uf7ff")
.get();
to
import * as functions from "firebase-functions";
import * as admin from "firebase-admin";
// eslint-disable-next-line max-len
export const searchGroceryItems = functions.https.onCall(async (data, context) => {
const {searchString, groceryType} = data;
const store = admin.firestore();
let query = store.collection("groceries");
if (groceryType !== "") {
query = query.where("type", "==", groceryType);
}
if (searchString !== "") {
query = query.where("title", ">=", searchString);
query = query.where("title", "<", searchString+"\uf8ff");
}
const querySnapshot = await query.get();
const groceryItems = querySnapshot.docs.map((doc) => ({
...doc.data(),
id: doc.id,
}));
return groceryItems;
});
This is not working as expected
I see payload in the request - {"data":{"searchString":"A","groceryType":"Nuts"}}
and the response in console is {"error":{"message":"INTERNAL","status":"INTERNAL"}}
How to fix this issue?
I have no idea on await api in this post - Conditional where clause in firestore queries