通过Axios将POST请求发送到Firebase Cloud Function

编程入门 行业动态 更新时间:2024-10-28 07:25:57
本文介绍了通过Axios将POST请求发送到Firebase Cloud Function的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我尝试向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

本文发布于:2023-10-11 22:14:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1482988.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:发送到   POST   Axios   Function   Cloud

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!