1

I have a React Native - Expo application, and I recently implemented a basic cloud function using the firebase-functions/v2/https onCall() like so:

exports.addmessage = onCall((request) => {
    logger.info("Request: " + request.data.text);
    return {text: request.data.text}
});

I deploy this function, and it gets deployed seemingly correct. Then I try to call this function from my client code in the App - component like so:

export default function App() {
  const addmessage = httpsCallable(functions, 'addmessage');
  
  function callFunction() {
    addmessage({ text: "Hello world!" })
      .then((result) => {
        console.log("Function called");
        console.log(result);
      }).catch((error) => {
        console.log("Function call failed");
        console.log(error)
      });
  }

  return (
    <SafeAreaProvider>
      <SafeAreaView style={styles.container}>
        <Button title="Call cloud function" onPress={() => callFunction()}></Button>
        <StatusBar style="auto" />
      </SafeAreaView>
    </SafeAreaProvider>
  );
}

When I click the button to call the cloud function, I get "Function call failed" and "[FirebaseError: internal]". Is this the correct way to call the callable function? I have checked the logger for the function inside the firebase portal, but no errors are logged there. What can I do to find/fix this issue? I have tested with an onRequest-function, and this works as expected.

I use "firebase": "9.22.1" as dependency in project-package.json (though I have also tested with "firebase": "9.20.0") In my functions package.json, I have these dependencies: "firebase-admin": "^11.8.0", "firebase-functions": "^4.3.1".

4
  • I have also posted a similar issue with onCall firebase function. It works perfectly fine in production but when testing I am getting this error. My posted question Commented Jun 1, 2023 at 15:59
  • Have you tried using the local emulator to test it?
    – Kiana
    Commented Jun 2, 2023 at 1:03
  • I have tried using local emulator and testing on my iOS phone, but same result. With the emulator running, I don't even get any logs from the emulator UI when trying to run the cloud function from my mobile. Though I can call the emulated function from Postman, and everything works perfectly. Commented Jun 2, 2023 at 12:38
  • I have also tried changing the emulator host to the IP of my pc. Then I should be able to view the emulator UI from my phone browser using the host + port right? This does not work either. Commented Jun 2, 2023 at 13:13

1 Answer 1

1

I didn't try your Cloud Function but with

exports.addmessage = onCall((request) => {
    logger.info("Request: " + request.data.text);
    return {text: text}
});

you actually don't declare the text variable.

The following should do the trick:

exports.addmessage = onCall((request) => {
    const text = request.data.text;
    logger.info("Request: " + text);
    return {text: text}   // Or return { text }
});

I also advise to better manage the error logging in your Cloud Function, by following the documentation: In the CF and in the client.

1
  • I tried to implement your solution and better logging, but still the same error message. Is this an indication that the connection between client and cloud functions are not correctly setup? Commented Jun 2, 2023 at 12:49

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.