0

I tried to get a collection of documents from firestore via firebase function.firebase function console displays the json data of documents and returned that data to android app but firebase function callable gets null value.can you help me how to receive the documents data in android app.

I even followed this Question for answer but still i receive null value in my app.

export const getproducts = functions.https.onCall((data, context)=>{
  let productarray = [];
  const productref = admin.firestore().collection("Products")
      .orderBy("product_id").limit(2);
  productref.get()
      .then((DataSnapshot) => {
        productarray=DataSnapshot.docs.map((doc) => {
          return doc.data();
        });
        console.log("products returned.", JSON.stringify(productarray));
        return JSON.stringify(productarray);
      }).catch((error)=> {
        throw new functions.https.HttpsError("unknown", error.message, error);
      });
});

my code for retreiving the data from android app

mFunctions = FirebaseFunctions.getInstance();
        return mFunctions
                .getHttpsCallable("getproducts")
                .call()
                .addOnSuccessListener(new OnSuccessListener<HttpsCallableResult>() {
                    @Override
                    public void onSuccess(HttpsCallableResult httpsCallableResult) {
                        try {
                            Gson g = new Gson();
                            String json = g.toJson(httpsCallableResult.getData());
                            ProductModel productModel = g.fromJson(json,ProductModel.class);
                            Log.e("getproducts",productModel.getProduct_id()); //i get null value here.
                        } catch (Exception e) {
                            Log.d("Error", e.toString());
                        }
                    }
                });

document that displayed in console:

10:49:59.240 AM
getproducts
products returned. [{"cutted_price":100,"dress_color":"blue","product_id":"000001"},{"cutted_price":500,"dress_color":"gray","product_id":"000002"}]

1 Answer 1

1

Your Cloud Function is missing a return statement.

// ...
productref.get()
  .then((DataSnapshot) => {
// ...

should be

// ...
return productref.get() // <----
  .then((DataSnapshot) => {
// ...

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.