参考:未定义的标题

发布于 2025-02-03 06:56:54 字数 1509 浏览 4 评论 0 原文

我正在使用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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

两个我 2025-02-10 06:56:54

要在试图获取文件之前,要将标头带有代币到Firebase,必须通过授权标头添加携带者令牌。
标题通常格式如下:

Authorization: Bearer <token>


当然,您的后端应分析相同的格式。

请记住,标题构造函数在Node.js环境中不可用。您需要从Node-fetch软件包中包含它,就像获取一样。要从获取功能中获取它,您可以使用破坏性的分配。

const fetch = require('node-fetch'); const { Headers } = fetch;

要创建新实例,请直接利用该属性。

let headers = new fetch.Headers();

有关更多信息,请参见 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:

Authorization: Bearer <token>

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.

const fetch = require('node-fetch'); const { Headers } = fetch;

To create a new instance, utilize the property directly.

let headers = new fetch.Headers();

For further information, see the node-fetch documentation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文