我正在使用Express Netlify-Lambda和Firebase构建API,但偶然发现了此错误。当我访问URL以获取数据时,我会获得参考器:未定义标题。因此,我发现这是Firebase的问题,但是我找不到我想要的正确信息。
因此,这就是我要做的,以下是我的导入:
const express = require('express');
const serverless = require('serverless-http');
const firebase = require("../resources/firebase"); // Import firebase config file
const bodyParser = require('body-parser');
const {doc, getDoc } = require("firebase/firestore");
这是路由器的一部分:
const app = express();
const router = express.Router();
router.get('/memes', async (req, res) => {
const apiKey = req.query.key;
const hashedApiKey = hashAPIKey(apiKey)
// This is where i get the error
const customerId = await getDoc(doc(firebase.db, "apiKeys", hashedApiKey));
const customer = await getDoc(doc(firebase.db, "customers", customerId.toString()));
if (!customer.active) {
res.sendStatus(403)
} else {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write('<h1>Hello from Express.js!</h1>');
res.end();
}
});
app.use(bodyParser.json());
app.use('/.netlify/functions/index', router);
module.exports = app;
module.exports.handler = serverless(app);
运行命令启动NetLify-lambda “ start”:“ NetLify-lambda Serve src src”
然后访问此URL http:// localhost:9000/.netlify/functions/index/memes?key = bla
该错误将被丢弃,Netlify-lambda将关闭。
我猜我需要在尝试获取文件之前将标题带有持有人令牌到Firebase。我也可能完全错了。无论如何,如果有人知道该怎么办,请告诉我。提前致谢。
I'm building an api using express netlify-lambda and firebase, but stumbled into this error. When I visit the url to get the data I get a ReferenceError: Headers is not defined. So I've figured out it was a problem with firebase, but I couldn't find the correct information I was looking for.
So this is what I'm trying to do, here are my imports:
const express = require('express');
const serverless = require('serverless-http');
const firebase = require("../resources/firebase"); // Import firebase config file
const bodyParser = require('body-parser');
const {doc, getDoc } = require("firebase/firestore");
Here's the part of the router:
const app = express();
const router = express.Router();
router.get('/memes', async (req, res) => {
const apiKey = req.query.key;
const hashedApiKey = hashAPIKey(apiKey)
// This is where i get the error
const customerId = await getDoc(doc(firebase.db, "apiKeys", hashedApiKey));
const customer = await getDoc(doc(firebase.db, "customers", customerId.toString()));
if (!customer.active) {
res.sendStatus(403)
} else {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write('<h1>Hello from Express.js!</h1>');
res.end();
}
});
app.use(bodyParser.json());
app.use('/.netlify/functions/index', router);
module.exports = app;
module.exports.handler = serverless(app);
After running the command to startup netlify-lambda "start": "netlify-lambda serve src"
and then visiting this url http://localhost:9000/.netlify/functions/index/memes?key=bla
the error will be thrown and netlify-lambda will shutdown.
I'm guessing I need to send headers with a Bearer token to firebase before trying to get a document. I could also be completely wrong. Anyway if anyone knows what to do please tell me. Thanks in advance.
发布评论
评论(1)
要在试图获取文件之前,要将标头带有代币到Firebase,必须通过授权标头添加携带者令牌。
标题通常格式如下:
在。
当然,您的后端应分析相同的格式。
请记住,标题构造函数在Node.js环境中不可用。您需要从Node-fetch软件包中包含它,就像获取一样。要从获取功能中获取它,您可以使用破坏性的分配。
要创建新实例,请直接利用该属性。
有关更多信息,请参见 node-fetch文档。
To send headers with a Bearer token to firebase before trying to get a document, Bearer Token must be added to Firebase via the Authorization header.
The header is usually formatted as follows:
This is documented in the OAuth 2.0 Authorization Framework: Bearer Token Usage, section 2.1.
Of course, your backend should parse the same format.
Keep in mind that the Headers constructor isn't available in the node.js environment. You'll need to include it from the node-fetch package, just like fetch. To get it from the fetch function, you can use a destructuring assignment.
To create a new instance, utilize the property directly.
For further information, see the node-fetch documentation.