0

For example I have dynamic filter list for my list of product, from that filter list send https request to cloud function. then trying to set Multiple conditional where clauses from that request

Multiple conditional where clauses work in local firebase app, but not inside cloud function -- Update Code --

import * as functions from "firebase-functions";
import * as admin from "firebase-admin";
import { Query } from "@google-cloud/firestore"; // update code
const cors = require("cors")({
  origin: true
});
admin.initializeApp();

const settings = { timestampsInSnapshots: true };
admin.firestore().settings(settings);

const database = admin.firestore();

export const getProduct = functions.https.onRequest(
  (request, response) => {
    return cors(request, response, async () => {
      try {
        let productRef = database.collection("product") as Query; // update code
        if(request.query.brand){
          productRef =productRef.where("brand", "==", request.query.brand)
        }
        if(request.query.lifeStyle){
          productRef =productRef.where("lifeStyle", "==", request.query.lifeStyle)
        }
        const productsQuery = await productRef.get();

        const products = productsQuery.docs.map(
          docRef => ({
            id: docRef.id,
            ...docRef.data()
          })
        );
        response.status(200).send(products);
      } catch (error) {
        response.status(500).send(error);
      }
  });
 }
);

after compile display error on console [ts] Type 'Query' is not assignable to type 'CollectionReference'. [2322]

1 Answer 1

4

TypeScript is complaining that you're trying to change the type of productRef. You are first assigning productRef with the result of a call to collection(). From the linked API docs, you can see that it returns a CollectionReference type object. Then, you're trying to reassign productRef with the result of a call to where(), which returns a different type Query. TypeScript doesn't like you to change the types of variables.

Since a CollectionReference is a subclass of Query, you can instead just tell TypeScript that you want to deal with productRef as a Query the entire time:

let productRef = database.collection("product") as Query

Note the cast to Query here.

Sign up to request clarification or add additional context in comments.

4 Comments

please can you tell me how to import Query to cloud function.
If you use VS Code, it will offer you the import automatically.
after update code VS code automatically update - import { Query } from "@google-cloud/firestore";
then I trying to deploy function error show in console Module '@google-cloud/firestore' is not listed as dependency in package.json , then I manual added @google-cloud/firestore to package.json , then run deploy function it successfully deploy. but after I send https request it not return any data or error

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.