可调用云函数错误:响应缺少数据字段 [英] Callable Cloud Function error: Response is missing data field
本文介绍了可调用云函数错误:响应缺少数据字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
不知道如何在 Flutter 中从云函数中获取响应.
Don't know how to get responce from cloud function in flutter.
我的云功能
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
exports.testDemo = functions.https.onRequest((request, response) => {
return response.status(200).json({msg:"Hello from Firebase!"});
});
我的颤振代码
///Getting an instance of the callable function:
try {
final HttpsCallable callable = CloudFunctions.instance.getHttpsCallable(
functionName: 'testDemo',);
///Calling the function with parameters:
dynamic resp = await callable.call();
print("this is responce from firebase $resp");
} on CloudFunctionsException catch (e) {
print('caught firebase functions exception');
print(e.code);
print(e.message);
print(e.details);
} catch (e) {
print('caught generic exception');
print(e);
}
flutter:捕获到 firebase 函数异常颤振:内部颤振:响应缺少数据字段.颤振:空
flutter: caught firebase functions exception flutter: INTERNAL flutter: Response is missing data field. flutter: null
推荐答案
使用
exports.testDemo = functions.https.onCall((data, context) => {
return {msg:"Hello from Firebase!"};
});
in cloud functions. Callable is different than Request
调用函数时需要添加参数:
When you call the functions you need to add the parameters:
改变:
// Calling a function without parameters is a different function!
dynamic resp = await callable.call();
到:
dynamic resp = await callable.call(
<String, dynamic>{
'YOUR_PARAMETER_NAME': 'YOUR_PARAMETER_VALUE',
},
);
如此处
然后打印响应:
print(resp.data)
print(resp.data['msg'])
Flutter 示例的 Firebase 函数这里和
The Firebase Functions for flutter example here and here
这篇关于可调用云函数错误:响应缺少数据字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文