【问题标题】:Flutter Firebase Functions: An error occured while calling functionFlutter Firebase Functions:调用函数时出错
【发布时间】:2021-07-16 22:25:21
【问题描述】:

我最近开始使用 Flutter 和 Firebase 开发应用程序。我使用 Firebase 模拟器来测试身份验证和云功能。我的大部分代码都在 Firebase Cloud Functions 中,我将其用于 Firestore 和 RTDB 的所有 CRUD。在添加一些新功能时,我在我的应用程序中遇到了这个错误。我尝试了很多搜索,但找不到任何解决方案。以下是收到的错误:

An error occured while calling function profile-get
Error Details: null
Message: An internal error has occurred, print and inspect the error details for more information.
Plugin: firebase_functions
Stacktrace: null

我在 Flutter 中的 API 类:

class Api {

  Api(this.functions);
  final FirebaseFunctions functions;

  static Api init() {
    FirebaseFunctions functions = FirebaseFunctions.instance;
    if (emulator) functions.useFunctionsEmulator(origin: host);
    return Api(functions);
  }

  Future<ApiResult> call(String name, {
    Map<String, dynamic> parameters,
  }) async {
    try {
      HttpsCallable callable = functions.httpsCallable(name);
      HttpsCallableResult results = await callable.call(parameters);
      return ApiResult(new Map<String, dynamic>.from(results.data));
    } on FirebaseFunctionsException catch (e) {
      print('An error occurred while calling function $name.');
      print('Error Details: ${e.details}\nMessage: ${e.message}\nPlugin: ${e.plugin}\nStacktrace: ${e.stackTrace}');
      return ApiResult({
        'status': 'error',
        'message': 'An error occured',
        'code': 'unknown'
      });
    }
  }

  static String get host => Platform.isAndroid ? 'http://10.0.2.2:2021' : 'http://localhost:2021';

}

我尝试直接从它们的本地 URL 运行这些函数,它们运行良好。

【问题讨论】:

  • 能把函数的代码分享给我们吗?你能确认这个函数被调用了吗?对我来说,错误似乎出在函数本身中。
  • 问题出在客户端。即使是最简单的功能也不起作用。我尝试使用它们的 URL 调用这些函数并且它们工作正常。但我无法追踪错误。
  • 您的函数是否有onCallonRequest 触发器?那些不一样。如果您可以使用 URL 调用它,则不能在您的应用程序中将其作为可调用函数调用。
  • 函数有onRequest触发器

标签: android ios firebase flutter google-cloud-functions


【解决方案1】:

正如 cmets defore 中所述,您正在使用 onRequest 重新创建云功能。这些不能使用 SDK 调用,只能通过 https URL 调用。

要创建可通过 Firebase SDK 调用的可调用函数,您需要重构函数以使用 onCall

它应该看起来像这样:

exports.yourFunctionName= functions.https.onCall((data, context) => {
  // receive the data
  const text = data.text;

  // return a response
  return {
    test:'test'
  }
});

Here你有更多关于可调用函数如何工作的信息。

【讨论】:

    【解决方案2】:

    您是否使用与标准 us-central1 不同的区域?这种情况经常发生,因此您需要更改您呼叫的地区

    HttpsCallable callable = FirebaseFunctions.instanceFor(region:"your_region").httpsCallable(name);
    

    【讨论】:

      猜你喜欢
      • 2021-06-02
      • 2018-08-14
      • 2019-11-23
      • 2020-03-19
      • 1970-01-01
      • 1970-01-01
      • 2022-01-15
      • 2021-08-29
      相关资源
      最近更新 更多