Home > Enterprise >  How to return data from firebase cloud function and return async response?
How to return data from firebase cloud function and return async response?

Time:09-04

What's up, I want return from firebase function data. My firebase function:

exports.genericEmail = functions.https.onCall(async (data, context) => {
  if (!context.auth && !context.auth.token.email) {
    throw new functions.https.HttpsError("failed-precondition", "Must be logged with an email address")
  }

  return "Hello"

})

My request from https:

const callFirebaseFunction = event => {
  const addMessage = httpsCallable(functions, 'genericEmail');
  addMessage()
    .then((result) => {
      console.log(result.data.output);
    }).catch((error) => {
      console.log(`error: ${JSON.stringify(error)}`);
    });
}

In firebase console written that function was executed: Firebase console I receive in JS console undefined. Also as function async I wanna get ability to track function's succeed/fail, how can I do that?

CodePudding user response:

You are returning a string from the Cloud Function so result.data will be a string and trying to read the property output will log undefined.

Try returning an object instead as shown below:

exports.genericEmail = functions.https.onCall(async (data, context) => {
  // function logic ...

  return { output: "Hello" }
})

Now result.data.output should log Hello on client side.

  • Related