【问题标题】:Callable Cloud Function error: Response is missing data field可调用云函数错误:响应缺少数据字段
【发布时间】:2020-01-02 19:13:05
【问题描述】:

不知道如何在 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 函数异常 颤振:内部 颤振:响应缺少数据字段。 颤振:空

【问题讨论】:

标签: firebase flutter dart google-cloud-functions


【解决方案1】:

使用

exports.testDemo = functions.https.onCall((data, context) => {
  return {msg:"Hello from Firebase!"};
});

在云函数中。 Callable 不同于 Request

调用函数时需要添加参数:

改变:

 // 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',
     },
);

如上所述here

然后打印响应:

print(resp.data)
print(resp.data['msg'])

Flutter 示例 herehere 的 Firebase 函数

【讨论】:

  • 我使用 onCall 并在我的颤振代码中得到了响应 print("这是来自 firebase $resp 的响应");因此,这是来自 'HttpsCallableResult' 的 firebase 实例和 firebase 控制台 URL 响应:{"error":{"message":"Bad Request","status":"INVALID_ARGUMENT"}}
  • @ParthBhanderi 我编辑了答案,向您展示了应该如何声明调用和响应。我相信你弄乱了调用参数,所以调用与你发布的函数不匹配。我希望它能解决你的问题
  • 是的,我遇到了问题,我的 firebase 功能工作正常。谢谢你的帮助。
猜你喜欢
  • 2021-08-29
  • 1970-01-01
  • 2012-02-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-07
  • 1970-01-01
  • 2015-07-22
相关资源
最近更新 更多