[英]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.