【问题标题】:Client Callable Firebase Function fails when deployed to regions other than us-central1部署到 us-central1 以外的区域时,客户端可调用 Firebase 函数失败
【发布时间】:2019-01-17 05:11:39
【问题描述】:

客户端可调用 Firebase 函数失败并显示“错误:无法读取数据,因为它的格式不正确。”当部署到 us-central1 以外的地区时(尝试过欧洲西部和亚洲)。

服务器代码

exports.getName = functions.region('europe-west1').https.onCall((data, context) => {
     return { name: "Testopoulos" };
});

客户代码 (React Native Firebase)

const httpsCallableUserName = firebase.functions().httpsCallable('getName');

getUserName = () => {
    httpsCallableUserName()
    .then(({ data }) => {
        console.log(data.name);
    })
    .catch(httpsError => {
        console.log(httpsError); 
    })
  }

docs 中有一条注释说“[当] 使用 HTTP 函数为托管提供动态内容时,您必须使用 us-central1”但我只是将一个对象返回给我们的前端 React Native 客户端而不是使用 Firebase 托管。我阅读了有关该地区的其他帖子,但我认为情况并非如此。有什么想法吗?

【问题讨论】:

  • 请编辑问题以显示客户端代码。
  • 感谢@DougStevenson 的评论我已经添加了客户端代码。
  • 尝试return { data : { name : "Testopulous" } }; 以确保返回值在发送到客户端时被正确包装。
  • @JamesPoag 结果相同。无论如何,如果它成功了,那就太奇怪了,因为它对我们只有 {name: "value"} 的 us-central1 工作得很好。
  • @RenaudTarnec 感谢您指出这一点!这听起来很有希望,但遵循该文档似乎并不像 var functions = firebase.functions('europe-west1'); 返回 Firebase: firebase.functions() takes either no argument or a Firebase App instance. (app/invalid-app-argument). 那样工作。

标签: node.js firebase google-cloud-functions


【解决方案1】:

根据documentation“客户端也可以指定区域,如果函数在us-central1以外的任何区域运行,则必须这样做。”。

正如您所发现的(参见上面的 cmets),它必须通过以下方式完成:

var functions = firebase.initializeApp(config).functions('europe-west1');

【讨论】:

    猜你喜欢
    • 2021-09-18
    • 2017-09-20
    • 2021-12-27
    • 2019-12-24
    • 2019-11-01
    • 2023-02-18
    • 2021-04-10
    • 2023-01-03
    • 2023-01-29
    相关资源
    最近更新 更多