【问题标题】:Firebase: How to run 'HTTPS callable functions' locally using Cloud Functions shell?Firebase:如何使用 Cloud Functions shell 在本地运行“HTTPS 可调用函数”?
【发布时间】:2018-10-10 16:44:31
【问题描述】:

我在Firebase official guides 中找不到此用例的解决方案。

  • 它们是 HTTPS 可调用函数
  • 想要使用 Cloud Functions shell 在本地运行 Functions 进行测试
  • 函数将接收到的数据保存到 Firestore
  • 还需要“auth”上下文信息

我的代码如下。提前致谢。


功能:

exports.myFunction = functions.https.onCall((data, context) => {
  const id = context.auth.uid;
  const message = data.message;

  admin.firestore()...
  // Do something with Firestore //
});

客户调用:

const message = { message: 'Hello.' };

firebase.functions().httpsCallable('myFunction')(message)
  .then(result => {
    // Do something //
  })
  .catch(error => {
    // Error handler //
  });

【问题讨论】:

  • 这在今天是不可能的。团队正在努力。
  • 如果您的目的是测试您的代码。我建议创建 2 个 firebase 项目 (1) 开发,您还可以进行一些集成测试(通常您会使用模拟数据抽象数据库调用)和 (2) 部署测试代码的生产项目。
  • 感谢您的回答。
  • 有没有办法将我的客户端(使用 firebase web SDK)连接到本地服务器而不是使用 shell?
  • @DougStevenson 什么时候准备好?

标签: firebase google-cloud-functions


【解决方案1】:

有一个专门用于此用例的 api,请参阅here

我在javascript(客户端)中使用它如下-

button.addEventListener('click',()=>{
//use locally deployed function
firebase.functions().useFunctionsEmulator('http://localhost:5001');
//get function reference
const sayHello = firebase.functions().httpsCallable('sayHello');
sayHello().then(result=>{
    console.log(result.data);
}) 
})

其中 sayHello() 是可调用的 firebase 函数。

当客户端是安卓模拟器/设备时。使用 10.0.2.2 代替 localhost。

flutter 的代码也是这样 -

CloudFunctions.instance.useFunctionsEmulator(origin: 'http://10.0.2.2:5000')
    .getHttpsCallable(functionName: 'sayHello')

【讨论】:

    【解决方案2】:

    云函数为此提供了模拟器。检查此link 它可以适合您的情况。它不是功能外壳,但出于测试目的,我认为它仍然可以为您工作

    【讨论】:

      猜你喜欢
      • 2022-12-03
      • 1970-01-01
      • 2020-06-06
      • 2019-03-08
      • 2023-04-03
      • 2021-10-09
      • 2020-10-01
      • 2020-05-25
      • 2018-07-15
      相关资源
      最近更新 更多