【问题标题】:Cannot get req.path and req.query.abc with firebase functions无法使用 firebase 函数获取 req.path 和 req.query.abc
【发布时间】:2019-05-26 01:28:26
【问题描述】:

我正在尝试在firebase functions 中获取请求查询参数和网址。 这是我正在使用的代码

firebase.json

{
    "hosting": {
        "public": "build",
        "rewrites": [{
            "source": "/getCoins",
            "function": "getCoins"
        }]
    }
}

package.json 中使用"firebase-functions": "^2.3.1"

functions/index.js

'use strict';
const functions = require('firebase-functions');
exports.getCoins = functions.https.onRequest((req, res) => {
    console.log(req.query); // [object Object]
    console.log(req.query.repeat); // empty
    console.log(req.url); // '/'
    console.log(req.originalUrl); // '/'
    res.sendStatus(200);
});

使用 firebase serve --only functions 在我的 Windows 命令提示符中启动 firebase 功能。当它开始从http://localhost:5000 提供数据时,我正在尝试请求http://localhost:5000/coins-app/us-central1/getCoins?repeat=4

我在命令提示符中没有收到任何错误,但只能看到上面 functions/index.js 代码中的注释行。

【问题讨论】:

    标签: firebase express request google-cloud-functions query-parameters


    【解决方案1】:

    您应该运行firebase serve 并请求http://localhost:5000/getCoins?repeat=4

    functions.https.onRequest() 不能直接接受查询字符串参数。

    【讨论】:

    • 谢谢!这也有帮助,但我可以通过将 firebase-tools 升级到 4.0.0 来让它工作
    • 你运行firebase serve了吗?不是firebase serve --only functions
    猜你喜欢
    • 2019-02-21
    • 2017-08-05
    • 1970-01-01
    • 1970-01-01
    • 2018-06-20
    • 1970-01-01
    • 2019-11-11
    • 2020-07-27
    • 1970-01-01
    相关资源
    最近更新 更多