简体   繁体   English

谷歌云功能:返回有效 JSON

[英]Google Cloud Functions: Return valid JSON

I´m trying to call a Google Cloud Function from my Flutter App using the cloud_functions package.我正在尝试使用 cloud_functions package 从我的 Flutter 应用程序调用 Google Cloud Function。

This is my Cloud Function:这是我的云 Function:

export const helloWorld = functions.region('europe-west1').https.onRequest((request, response) => {
response.status(200).json({
    message: "Hello World!"
  });
});

And this is my flutter method that calls this function:这是调用此 function 的我的 flutter 方法:

try {
  final dynamic resp =
      await CloudFunctions.instance.call(
    functionName: "helloWorld"
  );
  print(resp);

} on CloudFunctionsException catch (e) {
  ...
} catch (e) {
  ...
} finally {
  ...
}

As you can see it´s the most simply form of a request without any params.如您所见,这是最简单的请求形式,没有任何参数。

My problem: Each call to the Cloud Function results in a CloudFunctionsException.我的问题:每次调用 Cloud Function 都会导致 CloudFunctionsException。 Reason: " Response is not valid JSON object. ".原因:“响应无效 JSON object。 ”。

Maybe somebody has an idea what´s going wrong here?也许有人知道这里出了什么问题? If I call the cloud function via Postman or a browser, a valid JSON Object is returned and no exception is thrown.如果我通过 Postman 或浏览器调用云 function,将返回有效的 JSON Object 并且不会抛出异常。

Thanks in advance, Michael提前致谢,迈克尔

If you want to use the Flutter SDK to invoke a callable function, you need to actually define a callable function.如果要使用 Flutter SDK 调用可调用函数,则需要实际定义可调用函数。 Right now, you're declaring an HTTP function, which is not the same.现在,您正在声明一个 HTTP 函数,这与此不同。 Please read the documentation for callable functions to learn how to declare and implement a callable.阅读可调用函数的文档以了解如何声明和实现可调用函数

Instead of this:取而代之的是:

functions.https.onRequest(...)

It will look like this:它看起来像这样:

functions.https.onCall(...)

Then, you return a JavaScript object to convert to JSON, rather than using a response object.然后,您返回一个 JavaScript 对象以转换为 JSON,而不是使用响应对象。

I could find the bug: As soon as you define another region that the default one, the flutter package cloud_functions seems not to be able to find the function anymore:我可以找到错误:一旦您定义了默认区域的另一个区域,flutter 包 cloud_functions 似乎无法再找到该函数:

Works:作品:

export const helloWorld = functions.https.onCall((data, context) => {
    return {
        message: "Hello World"
    }
});

Doesn´t work:不起作用:

export const helloWorld = functions.region('europe-west1').https.onCall((data, context) => {
    return {
        message: "Hello World"
    }
});

I was having the same problem, and what worked for me was:我遇到了同样的问题,对我有用的是:

(Adding to @Michael 's answer) (添加到@Michael 的回答)

When declaring and calling the cloud function, it's important to specify the region in both cases.声明调用云函数时,在两种情况下都指定区域很重要。

My mistake was that I was only setting the region code on the function declaration.我的错误是我只是在函数声明中设置了区域代码。

More here: https://firebase.google.com/docs/functions/locations#client-side_location_selection_for_callable_functions .更多信息: https : //firebase.google.com/docs/functions/locations#client-side_location_selection_for_callable_functions

For Flutter you should specify the region in the region parameter of the singleton CloudFunctions :对于 Flutter,您应该在单例CloudFunctions的 region 参数中指定区域:

CloudFunctions(app: FirebaseApp.instance, region: "europe-west1")

In Flutter, to invoke a function with a different region, you'll need to use:在 Flutter 中,要调用具有不同区域的 function,您需要使用:

FirebaseFunctions.instanceFor(region: 'europe-west1') // Your region
  .httpsCallable('your_function')

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM