【问题标题】:Why firebase requests don't work on firebase serve?为什么 firebase 请求在 firebase 服务上不起作用?
【发布时间】:2020-01-16 02:24:51
【问题描述】:

每当我尝试使用 firebase serve 发出请求时,它都会失败。当我 firebase 部署我的函数然后发出请求时,一切正常,但通过 firebase serve 在本地运行相同的函数不起作用。我正在使用 Postman 发出请求。

我的步骤:我使用我的函数在文件夹中的 cmd 中运行 firebase serve。然后我复制我得到的响应链接并将其粘贴到 Postman,然后我使用此链接运行请求,例如 http://localhost:5000/workers-7baca/us-central1/api/workers。邮递员没有给我任何回应,我在控制台中收到错误。

我的函数代码:

app.get("/workers", (req, res) => {
  admin
    .firestore()
    .collection("workers")
    .get()
    .then(data => {
      res.json(data);
    })
    .catch(err => console.error(err));
});

当我从部署链接发出请求时,相同的代码对我有用,但它在本地主机上不起作用。

我收到类似FetchError: Unexpected error determining execution environment: network timeout 的网络超时错误。

有什么问题?

编辑:我在那里找到了一个可行的解决方案https://github.com/firebase/firebase-tools/issues/344

【问题讨论】:

  • 请编辑问题以准确显示您测试功能所经历的步骤。任何人都应该能够按照这些步骤重现问题。
  • 我已经更改了对问题的描述并添加了步骤。感谢您的建议
  • 传递该信息的具体内容是什么?邮差?还是从firebase serve 输出?你的函数的代码是什么?
  • 邮递员的消息是“无法得到任何响应。连接到时出错。”实际上,firebase 服务没有错误,我得到的唯一错误是我的捕获。我在主帖中添加了代码
  • 如果模拟器有问题,那么您应该将您的完整复制步骤发布到项目 GitHub。 github.com/firebase/firebase-tools

标签: firebase google-cloud-functions postman


【解决方案1】:
  1. 转到https://cloud.google.com/console/iam-admin/serviceaccounts
  2. 单击“创建服务帐户”,为其命名(例如模拟器),为其赋予项目>所有者角色。勾选“提供新的私钥”,选择“JSON”。
  3. 将文件保存在计算机上的某个位置
  4. 运行 export GOOGLE_APPLICATION_CREDENTIALS=”absolute/path/to/file.json”
  5. 运行 firebase serve --only 函数

【讨论】:

    【解决方案2】:

    安装 firebase 模拟器

    firebase emulators:start
    

    并使用生成的本地服务器连接到数据库

    【讨论】: