Flutter Firebase cloud_functions package throws error: INTERNAL

Viewed 900

When trying to make a call to a Cloud Function I get a "CloudFunctionsException"

  • The code of the exception is "INTERNAL
  • The message is "Response is not valid JSON object."

enter image description hereDescribe the bug

To Reproduce Steps to reproduce the behavior: - My call from the application is

 HttpsCallable _getName;

 _getName = CloudFunctions.instance.getHttpsCallable(functionName: 'getName',);


 try {
      HttpsCallableResult resp = await _getName.call(<String, dynamic>{'name': name,});
      Scaffold.of(context).showSnackBar(SnackBar(content: Text("${resp.data}")));
    } on CloudFunctionsException catch  (e) {
      showErrorMessage(context, 'Cloud functions exception with code: ${e.code}, and Details: ${e.details}, with message: ${e.message} ');
    } catch (e) {
      showErrorMessage(context, e.toString());
    }
  • My Cloud Function is written as so:
exports.getName = functions.https.onCall((data, context) => {
    return {
        "data" : "You hit the call at least!"
    };
});

Expected behavior In my response, I should get back the data: "You hit the test call". Instead, I get the error

Additional context When I make calls to the same function but with the HTTP package and receive it on the back end with "onRequest", it works.

  void _checkPersonsNameGET(String name)async{
    try {
      http.Response resp = await http.get(_cloudFunctionUrl,,  );
      _scaffoldKey.currentState.showSnackBar(SnackBar(content: Text("${resp.body}", style: TextStyle(color: Colors.green))));
    } catch (e) {
      showErrorMessage(context, e.toString());
    }
  }

  void _checkPersonsNamePOST(String name)async{
    try {
      http.Response resp = await http.post(_cloudFunctionUrl, body: { "name" : name } );
      _scaffoldKey.currentState.showSnackBar(SnackBar(content: Text("${resp.body}", style: TextStyle(color: Colors.green))));
    } catch (e) {
      showErrorMessage(context, e.toString());
    }
  }
exports.getName = functions.https.onRequest((request, response) => {

    const name = request.query.name || request.body.name;
    switch (name) {
        case 'Andrew':
            request.query.name ? response.send("Na he is the boi Q!") : response.send("Na he is the boi B!");
            break;

        case 'Brett':
            request.query.name ? response.send("Na just wierd! Q") : response.send("Na just wierd! B");
            break;

        case 'Eddie':
            request.query.name ? response.send("My brother but yeah! Q") : response.send("My brother but yeah! B");
            break;

        case 'James':
            request.query.name ? response.send("The biggest! Q") : response.send("The biggest! B");
            break;

        default:
            request.query.name ? response.send("Dunno who that is! Q") : response.send("Dunno who that is! B");
            break;
    }
});

It's a mock application and can be seen here https://github.com/earyzhe/firebase_cloud_functions_play

1 Answers

In my case, the cloud function was in a file that was not exported in the index file. Make sure your function is properly exported and has been deployed properly.

Related