【问题标题】:Send a POST request via Axios to a Firebase Cloud Function通过 Axios 向 Firebase Cloud Function 发送 POST 请求
【发布时间】:2018-09-13 23:01:39
【问题描述】:

我尝试向 Firebase 函数发送一个简单的请求,但每次都收到相同的错误... 显然,Firebase 函数没有从 Axios 请求中接收到我想要传输的数据。

这是 Firebase 功能:

[...] // Some imports

exports.completeProfile = functions.https.onRequest((req, res) => {

  // Debug
  console.log(req); 
  console.log(req.body);
  console.log(req.method);
  console.log("Test: " + userId + ", " + profilePicture + ", " + username);

  // We recover the data
  const userId = req.body.userId; // return "undefined"
  const profilePicture = req.body.profilePicture; // return "undefined"
  const username = req.body.username; // return "undefined"

  // we're checking to see if they've been transferred
  if (!userId || !profilePicture || !username) {
    // At least one of the 3 required data is not completed
    console.error("Error level 1: missing data");
    return res.status(400).send("Error: missing data");
  }

  [...] // (We have all the data, we continue the function)

});

这是我的 Axios 请求:

axios
    .post(
        '<FIREBASE CLOUD FUNCTION URL>',
        {
            userId: '12345667',
            profilePicture: 'https://profilepicture.com/url',
            username: 'test',
        }
    )
    .then(function(response) {
        console.log(response);
    })
    .catch(function(error) {
        console.log(error);
    });

当我运行 Axios 查询时,我总是遇到“网络错误”错误。这是 console.log(error); 给出的:

这里是服务器日志:

如何解决问题?感谢您的帮助。

【问题讨论】:

  • 您是否尝试过使用 Postman 复制相同的请求,并查看您得到的结果?
  • @gillyhl 好主意。该请求总是给出相同的错误。 (i.imgur.com/keQElQC.png)
  • @gillyhl 所以它应该来自 Firebase 函数,对吧?
  • 是的,那是firebase的功能,恢复数据后试试把测试控制台的日志放上去看看结果如何
  • 实际上,它看起来像是在尝试执行 OPTIONS 调用以及 POST。这将是一个 CORS 问题

标签: javascript rest firebase google-cloud-functions axios


【解决方案1】:

将您的 firebase 代码更改为此

var cors = require("cors");
completeProfileFn = (req, res) => {
  // Debug
  console.log(req);
  console.log(req.body);
  console.log(req.method);
  console.log("Test: " + userId + ", " + profilePicture + ", " + username);

  // We recover the data
  const userId = req.body.userId; // return "undefined"
  const profilePicture = req.body.profilePicture; // return "undefined"
  const username = req.body.username; // return "undefined"

  // we're checking to see if they've been transferred
  if (!userId || !profilePicture || !username) {
    // At least one of the 3 required data is not completed
    console.error("Error level 1: missing data");
    return res.status(400).send("Error: missing data");
  }

  // (We have all the data, we continue the function)
};

// CORS and Cloud Functions export logic
exports.completeProfile = functions.https.onRequest((req, res) => {
  var corsFn = cors();
  corsFn(req, res, function() {
    completeProfileFn(req, res);
  });
});

这是一个 CORS 问题。

【讨论】:

  • 终于找到了一个可行的解决方案,即将用 express 重写整个逻辑,但您的回答帮了大忙,谢谢 :D
猜你喜欢
  • 2019-01-27
  • 2018-04-25
  • 2021-08-20
  • 2018-11-02
  • 2019-04-26
  • 2014-07-28
  • 2019-11-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多