user9510058
user9510058

Reputation: 331

Firebase Callable function result always null in client despite successful execution

I am trying to execute a Callable function from android.

  1. Functions execution Log in console shows calls are received and executed without any error.
  2. Callable task in client receives the result after function execution, no exception reported.
  3. However, the received result is null.

Here is the function code-

const Translate = require('@google-cloud/translate');
const projectId = functions.config().firebase;

const translate = new Translate({
  projectId: projectId,
});

exports.translateMessage = functions.https.onCall((data, context) => {

  let text = data.text;
  let targetLanguage = data.target_language;
  let uid = context.auth.uid;

  console.log("incoming text: " + text)
  console.log("incoming uid: " + uid)
  console.log("incoming target: " + targetLanguage)

  //In future, check if uid or custom claim has quota
  if(!uid)
    return text;

  translate
  .translate(text, targetLanguage)
  .then(results => {
    const translation = results[0];

    console.log("translated text: " + translation)
    return Promise.resolve(translation);
    //also tried returning json like: return {translationText: translation};
  })
  .catch(err => {
    console.log(err)
    return Promise.reject(err);
  });
});

Here is the console log- enter image description here

As you'll see, right before returning the promise with translated text, it was logged to console successfully. I also tested by returning the translated text itself instead of a promise. Still null in client.

And here is the client code-

private Task<String> translateMessage(String text, String targetLanguage) {
    // Create the arguments to the callable function.
    Map<String, Object> data = new HashMap<>();
    data.put("text", text);
    data.put("target_language", targetLanguage);

    FirebaseFunctions mFunctions = FirebaseFunctions.getInstance();

    return mFunctions
            .getHttpsCallable("translateMessage")
            .call(data)
            .continueWith(new Continuation<HttpsCallableResult, String>() {
                @Override
                public String then(@NonNull Task<HttpsCallableResult> task) throws Exception {
                    String result = (String) task.getResult().getData();
                    Log.d(TAG, "Result in callable callback: " + result);
                    return result;
                }
            });
}

The result in callable callback is always null.

Any idea how to get the actual response?

Upvotes: 5

Views: 1137

Answers (1)

user9510058
user9510058

Reputation: 331

I have figured out a solution, my Javascript naivety is to blame.

Looks like only if I use two return statements, then the actual string is sent back to client. The return keyword in front of the actual promise was missing...

return translate
  .translate(text, targetLanguage)
  .then(results => {
    const translation = results[0];

    console.log("translated text: " + translation)
    return Promise.resolve(translation);
  })
  .catch(err => {
    console.log(err)
    return Promise.reject(err);
  });

Upvotes: 7

Related Questions