0

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

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
Kris Swat
  • 788
  • 1
  • 10
  • 39
  • Is that the only error you see? I suggest editing the question to show the entire set of logs from the function, as well as your client code that invokes this function, and its logs. You could also add more logging so it's perfectly clear what all of the inputs and outputs are at every stage. You should be able to narrow this down to the single line of code that causes a problem. – Doug Stevenson Jun 13 '23 at 18:29
  • I don't have emulator and my first try at these functions with firestore. As I am unable to see any other logs and cause, I am unable to handle the issue. Isn't that code and request (from developer tools) helpful enough? – Kris Swat Jun 13 '23 at 18:32
  • I see the issue popping up only when I enter some text in my search, which is searchString – Kris Swat Jun 13 '23 at 18:35
  • 2
    No, it's not helpful enough. That's why I'm asking for more. There should be enough information in the question that anyone can use the reproduce the problem as you observe it. – Doug Stevenson Jun 13 '23 at 18:43
  • I suspect there is something wrong in that splitting of the query. I see people using var, but the editor was complaining to use const or let. I am updating so cant use const. There are some queries written like - "let queryWebSDK = query(collection(db, "Collection"), conditionalConstraint)" – Kris Swat Jun 13 '23 at 19:39

0 Answers0