Firebase:如何运行HTTPS可调用功能'本地使用云功能shell?

时间:2018-04-30 14:51:52

标签: firebase google-cloud-functions

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

  • 它们是HTTPS可调用函数
  • 想要使用Cloud Functions shell在本地运行函数来测试
  • 功能将收到的数据保存到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 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

有一个完全适合此用例的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函数。

当客户端是android模拟器/设备时。使用10.0.2.2代替本地主机。

颤动的代码也是如此-

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