[英]I am trying to call firebase cloud functions from my flutter mobile application. I am able to call those from firebase functions : shell or locally
I am trying to call firebase cloud functions, from my flutter mobile application, initially I ran all the logic in Firestore logic in my mobile application and came to know that it's not secure to run those kind of stuff in client application.我正在尝试从我的 flutter 移动应用程序中调用 firebase 云功能,最初我在移动应用程序中运行 Firestore 逻辑中的所有逻辑,并开始知道在客户端应用程序中运行这些东西是不安全的。 So, now I am trying to implement on the logic in firebase cloud functions, but, I am stuck at this point from more than 3 to 4 days, also please comment correct way to call a callable firebase function with parameters.所以,现在我正在尝试在 firebase 云函数中实现逻辑,但是,我被困在这一点上超过 3 到 4 天,还请评论正确的方法来调用带有参数的可调用 firebase ZC1C42502768E68384FC1AB45 I tried by passing region to the cloud function and without passing region to the cloud functions, but both the methods failed.我尝试通过将区域传递给云 function 并且没有将区域传递给云函数,但是这两种方法都失败了。 please help me figure this out, or leave any suggestions in comments below, any help is appreciated and thanks in advance.请帮我解决这个问题,或者在下面的评论中留下任何建议,任何帮助表示赞赏并提前感谢。
The following are the codes I used in functions and flutter project:以下是我在函数和 flutter 项目中使用的代码:
My cloud functions (using Oncall):我的云功能(使用 Oncall):
import * as functions from "firebase-functions";
import { usersRef } from "./config/firebase";
const updateUserDataInDb = functions
.region("asia-south1")
.https.onCall((data, context) => {
console.log(data);
const userId = data.userId;
const gender = data.gender;
const phoneNumber = data.phoneNumber;
const currentTime = Date.now();
usersRef.doc(userId).update({
gender: gender,
phoneNumber: phoneNumber,
deviceId: "FCM",
});
return {
code: 200,
message: "success",
};
});
export { updateUserDataInDb };
my flutter code: I tried changing the code in multiple ways as people suggested in stack overflow and GitHub on other question's, but nothing worked in my case.我的 flutter 代码:我尝试按照人们在堆栈溢出和 GitHub 中的建议以多种方式更改代码,但在我的情况下没有任何效果。 Please help me overcome this issue, Thanks in advance.请帮我解决这个问题,在此先感谢。
method 1 :
updateuserdata() async {
print('invoking function');
var params = {
"userId": "1234",
"gender": "male",
"phoneNumber": "9999999999",
};
try {
HttpsCallable callable =
FirebaseFunctions.instanceFor(region: "asia-south1").httpsCallable(
'updateUserDataInDb',
options: HttpsCallableOptions(timeout: Duration(seconds: 5)));
dynamic result = callable.call(params).catchError((onError) {
print('function failed');
});
print(result);
} catch (error) {
print(error);
}
}
method 2 :
updateuserdata() async {
print('invoking function');
var params = {
"userId": "1234",
"gender": "male",
"phoneNumber": "9999999999",
};
try {
HttpsCallable callable =
FirebaseFunctions.instanceFor(region: "asia-south1").httpsCallable(
'updateUserDataInDb',
options: HttpsCallableOptions(timeout: Duration(seconds: 5)));
dynamic result = callable(params);
print(result);
} catch (error) {
print(error);
}
}
method 3 :
updateuserdata() async {
print('invoking function');
var params = {
"userId": "1234",
"gender": "male",
"phoneNumber": "9999999999",
};
try {
HttpsCallable callable =
FirebaseFunctions.instance.httpsCallable(
'updateUserDataInDb',
options: HttpsCallableOptions(timeout: Duration(seconds: 5)));
dynamic result = callable.call(params);
print(result);
} catch (error) {
print(error);
}
}
method 4:
updateuserdata() async {
print('invoking function');
var params = {
"userId": "1234",
"gender": "male",
"phoneNumber": "9999999999",
};
try {
HttpsCallable callable =
FirebaseFunctions.instanceFor(app : Firebase.app(), region: "asia-south1").httpsCallable(
'updateUserDataInDb',
options: HttpsCallableOptions(timeout: Duration(seconds: 5)));
dynamic result = callable.call(params).catchError((onError) {
print('function failed');
});
print(result);
} catch (error) {
print(error);
}
}
Errors I am getting:我得到的错误:
Error 1 :
PlatformException (PlatformException(3840, The data couldn’t be read because it isn’t in the correct format., {message: The data couldn’t be read because it isn’t in the correct format., code: 3840}, null))
Error 2 :
Exception has occurred. PlatformException (PlatformException(not-found, NOT FOUND, {message: NOT FOUND, code: not-found}, null))
Error 3:
Error: Value for argument "documentPath" is not a valid resource path. Path must be a non-empty string.
Error 4:
throw PlatformException(code: errorCode, message: errorMessage as String?, details: errorDetails, stacktrace: errorStacktrace);
You are defining params
but never send it to your cloud function so the cloud function will wail internaly because it dan't find the paths it expects.您正在定义params
,但从不将其发送到您的云 function 因此云 function 将在内部哀号,因为它找不到它期望的路径。
It looks like you're not await
ing your call()
functions:看起来您并没有await
您的call()
函数:
dynamic result = await callable.call(params)
.catchError((onError) {
print('function failed');
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.