我们如何解决Flatter应用程序中的firebase函数内部异常?
我正在使用firebase firestore和firebase函数开发一个颤振应用程序。我一次又一次地遇到这个异常-我们如何解决Flatter应用程序中的firebase函数内部异常?,firebase,flutter,google-cloud-firestore,google-cloud-functions,Firebase,Flutter,Google Cloud Firestore,Google Cloud Functions,我正在使用firebase firestore和firebase函数开发一个颤振应用程序。我一次又一次地遇到这个异常- [ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: [firebase_functions/internal] INTERNAL E/flutter (15454): #0 catchPlatformException (package:cloud_functions_platform_i
[ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: [firebase_functions/internal] INTERNAL
E/flutter (15454): #0 catchPlatformException (package:cloud_functions_platform_interface/src/method_channel/utils/exception.dart:19:3)
从过去几个小时以来,我一直试图解决此异常,但无法取得任何进展。这是index.js文件中我的函数的代码
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.addUser = functions.https.onCall(async (data) => {
await admin.firestore().collection("collection_name").add({
name: "my_name",
email: "my_email"
}
);
});
这是我的飞镖程序代码
MyButton(
onPressed: () async {
HttpsCallable a =
FirebaseFunctions.instance.httpsCallable("addUser");
final x = await a();
print(x.data);
},
),
提前谢谢 我以为你只是指客户方。但是为了重述Yadu写的内容,您也应该在云函数中处理它。大概是这样的:
exports.addUser = functions.https.onCall(async (data) => {
try {
await admin.firestore().collection("collection_name").add({
name: "my_name",
email: "my_email"
}
);
} catch (err) {
throw new functions.https.HttpsError('invalid-argument', "some message");
}
});
在客户端:
HttpsCallable a = FirebaseFunctions.instance.httpsCallable("addUser");
try {
final x = await a();
print(x.data);
} on FirebaseFunctionsException catch (e) {
// Do clever things with e
} catch (e) {
// Do other things that might be thrown that I have overlooked
}
你可以在网上多读一点
客户端描述可在“在客户端处理错误”一节中找到。内部异常是因为您没有在函数中处理错误,请在函数中使用try-catch,在try中封装您的整个逻辑,捕获它并重新播放
functions.https.HttpsError
,firebase将相应地处理该错误,这是因为firebase希望“HttpsError”不会出现任何其他错误,这些错误最终会成为客户端的内部错误您必须捕获错误并将其作为函数重新发送。https.HttpsError
。这不是什么新鲜事。从字面上说,每个人的答案在这个线程包括这一点。Howewer我最近发现的是对HttpsError错误代码的特殊定义要求。
它似乎不是任何字符串,而是functionErrorCode
enum的一个值。此处列出了可能的值:
"ok" | "cancelled" | "unknown" | "invalid-argument" | "deadline-exceeded" | "not-found" | "already-exists" | "permission-denied" | "resource-exhausted" | "failed-precondition" | "aborted" | "out-of-range" | "unimplemented" | "internal" | "unavailable" | "data-loss" | "unauthenticated"
更多关于:
希望这对某人有所帮助,尽管这不是一个新问题。谢谢你的帮助,已经尝试过了,但是错误消息没有帮助[firebase_functions/internal]internal。我编辑了我的回答,并附上了Yadu的意思。如果这不起作用,请确保将代码发布到处理异常的位置。我现在已将所有内容放入try-catch块中,并在index.js文件中抛出https错误。但我认为我的代码还有其他一些问题,当我注释掉除admin.firestore()之外的所有内容时,仍然会出现错误。你能告诉我为什么会发生这种情况吗?我已经将我的dart代码放在try catch块中,并将index.js代码与functions.https.HttpsError放在一起,但我仍然收到Firebase内部错误。我正在使用nodjs8运行时