可调用的云函数错误:响应缺少数据字段

Par*_*eri 8 dart firebase flutter google-cloud-functions

不知道如何从颤振中的云函数中获得响应。

我的云功能

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!"});
 });

Run Code Online (Sandbox Code Playgroud)

我的颤振代码

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

Run Code Online (Sandbox Code Playgroud)

颤振:捕获了 Firebase 函数异常颤振:内部颤振:响应缺少数据字段。颤振:空

ald*_*aie 8

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

在云函数中。CallableRequest不同

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

改变:

 // Calling a function without parameters is a different function!
  dynamic resp = await callable.call();
Run Code Online (Sandbox Code Playgroud)

到:

dynamic resp = await callable.call(
     <String, dynamic>{
       'YOUR_PARAMETER_NAME': 'YOUR_PARAMETER_VALUE',
     },
);
Run Code Online (Sandbox Code Playgroud)

描述在这里

然后打印响应:

print(resp.data)
print(resp.data['msg'])
Run Code Online (Sandbox Code Playgroud)

此处此处的 Firebase Functions for flutter 示例


Rog*_*mao 7

您必须明确地将属性“data”放入响应的 json 中。

喜欢:

response.send({
"status" : success,
"data" : "some... data"
});
Run Code Online (Sandbox Code Playgroud)