通过 Axios 向 Firebase 云函数发送 POST 请求 [英] Send a POST request via Axios to a Firebase Cloud Function

查看:31
本文介绍了通过 Axios 向 Firebase 云函数发送 POST 请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I try to send a simple request to a Firebase function, but I get the same error every time... Apparently, the Firebase function does not receive the data I want to transmit from the Axios request.

这是 Firebase 函数:

This is the Firebase function :

[...] // 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 请求:

And here is my Axios request :

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); 给出的内容:

When I run the Axios query, I always come across the "Network Error" error. Here is what console.log(error); gives :

这里是服务器日志:

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

How to solve the problem? Thanks for your help.

推荐答案

将您的 firebase 代码更改为此

change your firebase code to this

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 问题.

It is a CORS issue.

这篇关于通过 Axios 向 Firebase 云函数发送 POST 请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆