【问题标题】:firebase functions emulator useFunctionsEmulator() method not workingfirebase 函数模拟器 useFunctionsEmulator() 方法不起作用
【发布时间】:2020-01-01 05:24:46
【问题描述】:

我目前正在尝试在本地测试我的云功能。 我找到了几种方法,但使用 firebase 模拟器和 useFunctionsEmulator() 方法似乎很棒。在https://firebase.google.com/docs/functions/local-emulator,他们没有说方法,但我在这个网址How to test `functions.https.onCall` firebase cloud functions locally?上找到了。

但是,当我运行 firebase emulator:start 和 console.log(firebase.functions().useFunctionsEmulator('http://localhost:5001') 时,它只是显示未定义。

我在原点上尝试了几个输入,但没有任何改变。互联网上关于此的信息很少,我认为那是因为这是 alpha,所以请帮助我。

【问题讨论】:

  • 我有同样的问题
  • 你能多展示一些你的代码吗?预计useFunctionsEmulator 返回未定义,它只是更改设置。但是,下次您调用“可调用”函数时,它应该与模拟器对话,而不是与生产环境对话。
  • 这方面有什么更新吗?

标签: firebase google-cloud-functions


【解决方案1】:

在客户端初始化 firebase 应用后,我通过调用 useFunctionsEmulator() 方法让模拟器工作并处理本地请求。调用它之前会导致错误。

firebase.initializeApp(config);
firebase.functions().useFunctionsEmulator("http://localhost:5001");

【讨论】:

  • 好吧,就像其他人回答的那样, useFunctionsEmulator 应该在 initializeApp 之后使用来设置东西。感谢您的回复,对于迟到的评论表示抱歉。
  • 每次调用functions()时是否需要调用useFunctionsEmulator(..)
【解决方案2】:

useFunctionsEmulator() 不返回任何内容,它只是一个 setter。

按以下方式使用:

firebase.initializeApp(config);

const functions = firebase.functions();
functions.useFunctionsEmulator("http://localhost:5001");

【讨论】:

  • 它对我有用,这应该是公认的答案!
【解决方案3】:

我也无法获得useFunctionsEmulator(),但我有一个解决方法:

我将onCall 函数切换为onRequest 函数,如下所示:

// FROM THIS
exports.exampleFunction = functions.https.onCall((data, context) => {
  // CODE FOR CLOUD FUNCTION
});
// TO THIS
exports.exampleFunction = functions.https.onRequest((request, response) => {
  // CODE FOR CLOUD FUNCTION
});

然后我可以使用此命令在本地提供我的功能firebase serve --only functions,它将显示一个url,我可以通过curl、邮递员或我的浏览器向它发送请求。当我编辑完函数后,我将它切换回onCall 函数。我希望这有帮助!

【讨论】:

    【解决方案4】:

    正如@app_ 所写,但这对某人来说可能是值得的:

    如果您使用区域,它们应该只为在线呼叫设置,而不是模拟的。这对我有用(JavaScript 客户端):

    const fns = LOCAL ? firebase.app().functions() :
      firebase.app().functions(functionsRegion);
    
    const log = fns.httpsCallable('logs_v200719');
    

    LOCAL 之前设置为true,如果我们知道我们是针对模拟后端运行的。请让我知道是否有从 Firebase 客户端嗅探的标准方法。

    为了开发者的经验,最好在本地以相同的方式处理区域,这意味着不需要上述三元运算符。

    firebase-tools 8.6.0,JavaScript 客户端 7.16.1

    【讨论】:

      【解决方案5】:

      打电话时遇到问题

      firebase.functions().useFunctionsEmulator("http://localhost:5001");

      您可以通过添加此行在您的应用中测试您的 onCall 方法。

      FirebaseFunctions functions = FirebaseFunctions.getInstance();
              functions.useEmulator("10.0.2.2", 5001);
      

      functions.UseFunctionsEmulator("http://localhost:5004");
      

      这是我的来源:https://firebase.google.com/docs/functions/local-emulator

      编辑:也可以在 node.js 中使用

      const admin = require('firebase-admin');
      admin.initializeApp();
      admin.database().useEmulator('127.0.0.1', 5001);
      

      【讨论】:

        猜你喜欢
        • 2020-10-11
        • 1970-01-01
        • 1970-01-01
        • 2018-04-24
        • 2013-12-29
        • 1970-01-01
        • 2017-09-19
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多