简体   繁体   English

如何从 Flutter 上的 FirebaseFunctions 向 Firebase 函数发送参数

[英]How to send parameters to Firebase Functions from FirebaseFunctions on Flutter

I am using a onRequest function in Firebase Functions to send an email with nodemailer.我在 Firebase 函数中使用 onRequest 函数通过 nodemailer 发送电子邮件。

Testing the function using a https:// request in my browser works perfectly fine if I pass in the parameters at the end in the format ?senderName=Bob如果我在最后以?senderName=Bob格式传入参数,则在浏览器中使用 https:// 请求测试该功能可以正常工作

In my function I use req.query.senderName to retrieve the variables.在我的函数中,我使用req.query.senderName来检索变量。

When I use the Flutterfire cloud_functions dart plugin I cannot get my variables to pass to my function.当我使用 Flutterfire cloud_functions dart 插件时,我无法将变量传递给我的函数。 Below is my code:下面是我的代码:

  Future<void> sendEmail() async {
    FirebaseFunctions functions = FirebaseFunctions.instance;
    //TODO:remove when deployed
    //functions.useFunctionsEmulator(origin: 'http://localhost:5001');
    HttpsCallable callable = functions.httpsCallable('sendEmail');
    try {
      final HttpsCallableResult result = await callable.call(
        {
          'senderName': contactName,
        },
      );
      print(result.data);
    } on FirebaseFunctionsException catch (e) {
      print('Firebase Functions Exception');
      print(e.code);
      print(e.message);
    } catch (e) {
      print('Caught Exception');
      print(e);
    }
  }

I've logged the value of 'req' in the firebase function and it returns the following lines (most lines removed for clarity)我已经在 firebase 函数中记录了“req”的值,它返回以下几行(为了清楚起见,删除了大部分行)

query: {},
...
body: { data: { senderName: 'Bob' } },
...

Is there something I'm doing wrong for my variables to end up in 'body' and not 'query' or is this a bug?我的变量最终出现在“正文”而不是“查询”中是不是我做错了什么,还是这是一个错误?

It's not intended to mix onRequest type HTTP functions with the Firebase SDK that uses callable functions.不应将 onRequest 类型的 HTTP 函数与使用可调用函数的 Firebase SDK 混合使用。 The SDK for callable functions is only meant to call onCall type functions, as described in the documentation .可调用函数的 SDK 仅用于调用 onCall 类型的函数,如文档中所述。 Callable functions functions have a special protocol , and the SDK hides all of those details from you.可调用函数函数有一个特殊的 协议,SDK 对你隐藏了所有这些细节。

If you don't want to write an onCall function, and instead need to use onRequest, then you should not use the Firebase SDK to invoke it.如果您不想编写 onCall 函数,而是需要使用 onRequest,那么您不应该使用 Firebase SDK 来调用它。 You should use a normal HTTP client for Flutter.你应该为 Flutter 使用一个普通的 HTTP 客户端。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Flutter Firebase:从云函数发送通知 - Flutter Firebase: Send notifications from cloud functions 如何将列表作为参数发送到Flutter中的Firebase函数? - How to send a list as a parameter to Firebase Functions in Flutter? 如何从 firebase 向 flutter 中的用户发送数据? - How to send data from firebase to the user in flutter? 使用 Firebase 托管将参数发送到 Cloud Functions - Send parameters to Cloud Functions with Firebase Hosting 如何从云函数中的请求中获取参数? 火力地堡 - How to get parameters from request in cloud functions? Firebase (颤振)如何使用 Firebase 的 Cloud Functions 从 Firestore 检索文档 - (Flutter) How to retrieve document from Firestore using Cloud Functions for Firebase 如何使 FirebaseFunctions.instance.httpsCallable() 通过授权 header? (使用颤振) - How to make FirebaseFunctions.instance.httpsCallable() pass Authorization header? (Using Flutter) Flutter + Firebase云功能-如何从HttpsCallableResult获取数据 - Flutter + Firebase Cloud Functions - How to get data from HttpsCallableResult 如何从 Flutter 调用本地 Firebase 函数(在模拟器中)? - How can I call local Firebase Functions (in Emulator) from Flutter? 如何免费使用 flutter firebase 功能 - How to use flutter firebase functions for free
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM