【问题标题】:Firebase Functions and external node packagesFirebase 函数和外部节点包
【发布时间】:2020-09-12 12:18:38
【问题描述】:

./functions/index.js 中,我需要几个外部包(node_modules),例如:jsonwebtoken、uuid 或 escapeHtml,但它们不能像 在浏览器中那样工作强> 我明白了:

“错误:无法处理请求”

问题:我怎样才能让它发挥作用?

./functions/index.js:

const functions = require("firebase-functions");
const jwt = require("jsonwebtoken");

exports.getToken = functions.https.onRequest(async (request, response) => {
  const token = await jwt.sign({ id: 3333 }, "secret");
  response.send("Hello from Firebase! + ", token);
});

PS:在文档中它说它假设允许外部包甚至本地包 https://firebase.google.com/docs/functions/handle-dependencies

package.json:

{
  "name": "functions",
  "description": "Cloud Functions for Firebase",
  "scripts": {
    "serve": "firebase emulators:start --only functions",
    "shell": "firebase functions:shell",
    "start": "npm run shell",
    "deploy": "firebase deploy --only functions",
    "logs": "firebase functions:log"
  },
  "engines": {
    "node": "8"
  },
  "dependencies": {
    "firebase-admin": "^8.9.0",
    "firebase-functions": "^3.3.0",
    "jsonwebtoken": "^8.5.1",
    "uuid": "^8.1.0"
  },
  "devDependencies": {
    "firebase-functions-test": "^0.1.6"
  },
  "private": true
}

【问题讨论】:

  • 如果错误发生在浏览器中,则编辑问题以显示浏览器代码和出错的特定代码行。您展示的只是 Cloud Function 代码,它完全在后端运行,不以任何方式涉及浏览器。浏览器代码只会调用这个函数并接收响应。
  • 嗨,Doug,正如代码中所示,浏览器应该显示“Hello from Firebase + a generated token”,但它出现在“/”上的是“错误:无法处理请求”。
  • 我不清楚你在观察什么。正如我所说,编辑问题以显示浏览器代码。我们需要能够看到您所做的一切。
  • 你能分享你的package.json
  • @DougStevenson 浏览器代码是指浏览器中呈现的代码(通过查看页面源代码)?

标签: javascript firebase jwt google-cloud-functions node-modules


【解决方案1】:

查看http://console.firebase.google.com仪表板我发现在functions/logs中有这个错误:

未配置结算帐户。外部网络无法访问,配额受到严格限制。配置结算帐号以移除这些限制

我认为这意味着使用免费的 firebase 帐户 (Spark) 将不允许我运行外部 node_packages。但这可能适用于现收现付计划 (Blaze)。

有 Blaze 计划的人可以在他们的 functions/index.js 文件中确认外部节点包与 firebase 一起使用吗?

【讨论】:

    猜你喜欢
    • 2019-05-26
    • 1970-01-01
    • 2017-10-28
    • 1970-01-01
    • 2017-06-08
    • 1970-01-01
    • 2020-12-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多