【发布时间】:2020-01-31 09:43:09
【问题描述】:
尝试调用云函数时,我收到“CloudFunctionsException”
- 异常代码为“INTERNAL”
- 消息是“响应不是有效的 JSON 对象。”
复制 重现行为的步骤: - 我的应用程序调用是
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 是这样写的:
exports.getName = functions.https.onCall((data, context) => {
return {
"data" : "You hit the call at least!"
};
});
预期行为 在我的回复中,我应该取回数据:“你打了测试电话”。相反,我得到了错误
附加上下文 当我使用 HTTP 包调用相同的函数并在后端使用“onRequest”接收它时,它可以工作。
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;
}
});
这是一个模拟应用程序,可以在这里看到 https://github.com/earyzhe/firebase_cloud_functions_play
【问题讨论】:
-
当您看到 INTERNAL 时,它是否包含数字?假设它看起来像这样:INTERNAL(1)?如果有,显示的数字是多少?
-
嘿。没有号码。在详细信息中,它说“空”。我认为那是代码应该在的地方。我在上面添加了一张图片。谢谢
-
谢谢!我问了这个数字,因为那些“内部”错误通常带有一个数字。如果你遇到过这些,这里是那些代码原型的映射github.com/googleapis/googleapis/blob/master/google/rpc/… 现在,在抛出的错误中你看到一个“状态”字段吗?这就像cloud.google.com/apis/design/errors#http_mapping
-
从上图中可以看出,抛出的错误中没有任何状态。事实上,如果你查看包含在 Dart“cloud_functions”包中的“CloudFunctionsException”类,它没有状态值。 pub.dev/documentation/cloud_functions/latest/cloud_functions/…
-
进一步看,它不是从“https_callable.dart”文件中的响应映射的。
标签: firebase http flutter dart google-cloud-functions