【发布时间】: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