Describe the bug
When trying to make a call to a cloud function I get a "CloudFunctionsException"
To Reproduce
Steps to reproduce the behavior:
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());
}
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
I have the same error. Do you have a solution?
Thank you.
I encountered same error, but when you define the region the function is deployed in, it works. In flutter you can do:
final CloudFunctions cf = CloudFunctions(region: 'europe-west1');
final HttpsCallable _getName = cf.getHttpsCallable(
functionName: 'getName',
);
dynamic resp = await _getName.call(<String, dynamic>{
'name': 'YourName'
});
I'm having the same problem. Adding the region didn't help is there any update? All other firebase services work including auth and firestore. Also my functions work when called from the shell. My code is identical to the examples and I'm still getting this error. Any suggestions would be greatly appreciated.
I'm also having the same problem as jfkback!
@jgadsby I got the function working as it should. What I did was move the call inside a build method for a widget. Previously, I had it in a separate database interaction file. It seems that the build context is needed in some way for the function to find the correct app or perhaps to verify firebase auth. Not totally sure where the problem happens, but it seems to be in the Android implementation itself. If anyone knows a way to make it work without it being in build please let me know.
Oh interesting! I had it as something launched from a button. I actually just went the route of manually hitting the endpoint using the dart http library and passing headers in as arguments. It sounds like you could pass the BuildContext into that separate file to keep the functionality you want.
Any update on this issue?
Hi,
I've tested this on the latest roadmap version, and I believe this has been resolved
try {
HttpsCallableResult callable = await CloudFunctions.instance
.useFunctionsEmulator(origin: 'http://10.0.2.2:5001')
.httpsCallable('getName').call();
print(callable.data);
} on CloudFunctionsException catch (e) {
print("Cloud functions exception with code: ${e.code}, and Details: ${e.details}, with message: ${e.message}");
} catch (e) {
print("ERROR: $e");
}
This outputs {data: You hit the call at least!}
Please keep an eye out for a release, and let us know if this is still an issue
I'm still experiencing this problem.
RaisedButton(onPressed: () async {
await CloudFunctions.instance
.getHttpsCallable(functionName: 'helloWorld')
.call();
}),
import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';
admin.initializeApp();
export const helloWorld = functions.https.onCall((data, context) => {
// functions.logger.info("Hello logs!!!", { structuredData: true });
return {
message: "Hello World"
}
});
Results in:
Unhandled Exception: PlatformException(functionsError, Cloud function failed with exception., {code: INTERNAL, details: null, message: Response is not valid JSON object.})
I'm still experiencing this problem.
RaisedButton(onPressed: () async { await CloudFunctions.instance .getHttpsCallable(functionName: 'helloWorld') .call(); }),import * as functions from 'firebase-functions'; import * as admin from 'firebase-admin'; admin.initializeApp(); export const helloWorld = functions.https.onCall((data, context) => { // functions.logger.info("Hello logs!!!", { structuredData: true }); return { message: "Hello World" } });Results in:
Unhandled Exception: PlatformException(functionsError, Cloud function failed with exception., {code: INTERNAL, details: null, message: Response is not valid JSON object.})
Changing
export const helloWorld
to
exports.helloWorld
seems to have done the trick.
Most helpful comment
I'm having the same problem. Adding the region didn't help is there any update? All other firebase services work including auth and firestore. Also my functions work when called from the shell. My code is identical to the examples and I'm still getting this error. Any suggestions would be greatly appreciated.