【发布时间】: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