我尝试向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 exportspleteProfile = 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: 'profilepicture/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 exportspleteProfile = functions.https.onRequest((req, res) => { var corsFn = cors(); corsFn(req, res, function() { completeProfileFn(req, res); }); });这是一个CORS问题.
It is a CORS issue.
更多推荐
通过Axios将POST请求发送到Firebase Cloud Function
发布评论