Home > Enterprise >  How do I call a Google Cloud Function from ReactJs and pass an argument into it?
How do I call a Google Cloud Function from ReactJs and pass an argument into it?

Time:11-24

I have an existing Google Cloud Function that I am trying to call. I want to pass into it an Id and with that Id, the cloud function can run its operation in the background. I am new to cloud functions and would appreciate the help. When I call the function, I get the error "POST....500" and the error message says "INTERNAL". Here is the code I use to call the cloud function, I have declared a function that I call in an onClick event in a button:

import { httpsCallable } from "firebase/functions";

import { functions } from "../../../firebase/firebase";

/* Below is how I call the cloud function */

// handle payment confirmation
  const handlePayment = async () => {
    const confirmPayment = httpsCallable(
      functions,
      "loanV2Group-onManualRepay"
    );
    confirmPayment(loanId)
      .then((response) => {
        if (response.status === "success") {
          setIsConfirmed(true);
          setOpenSnackbar(true);
          handleClose();
        } else {
          setOpenSnackbar(true);
        }
      })
      .catch((error) => {
        console.log("Payment Confirmation Error: ", error);
      });
  };

Kindly help!! Thanks.

CodePudding user response:

As per documentations Cloud functions in app are called with arguments like as follows

const confirmPayment = httpsCallable(
         functions, 
         "loanV2Group-onManualRepay"
      );
      confirmPayment({ variable: your_variable })
      .then((response) => {
                if (response.status === "success") {
                    setIsConfirmed(true);
                    setOpenSnackbar(true);
                    handleClose();
                } else {
                    setOpenSnackbar(true);
                }
        })
        .catch((error) => {
                console.log("Payment Confirmation Error: ", error);
        });

According to the same documentation, it will work for cloud functions as well. The parameters for cloud functions are received in HTTP responses. You can use JSON-like syntax to send those arguments to the cloud function.

CodePudding user response:

It sounds like maybe the error is on the cloud side, not your calling code. Give that a look...

  • Related