如何从 Firebase Cloud Function 发出 Axios 请求

编程入门 行业动态 更新时间:2024-10-27 19:22:54
本文介绍了如何从 Firebase Cloud Function 发出 Axios 请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在 Firebase Cloud Function 中尝试了以下方法来执行 Axios 请求,但没有成功.

I've tried the following in Firebase Cloud Function to do an Axios request but it didn't work.

const functions = require('firebase-functions'); const axios = require('axios'); const cors = require('cors')({ origin: true }); exports.checkIP = functions.https.onRequest((req, res) => { cors(req, res, () => { if( req.method !== "GET" ) { return res.status(401).json({ message: "Not allowed" }); } return axios.get('api.ipify?format=json') .then(data => { console.log(data) res.status(200).json({ message: data.ip }) }) .catch(err => { res.status(500).json({ error: err }) }) }) })

我还搜索了很多关于如何将 Axios 与 Cloud Functions 结合使用的示例,但没有找到.上面的代码没有返回任何东西.

I've also googled a lot for seeing some example of how to use Axios with Cloud Functions but found none. The above code is not returning anything.

有人可以帮忙吗?

P.S.:我已经在我的 Firebase 帐户中添加了账单明细,并且没有使用免费的 Spark 计划,而是使用 Blaze 计划.

P.S.: I've already added billing details in my Firebase account and not using the free Spark plan, rather using Blaze plan.

我终于能够使用 request-promise 节点包做到这一点,但仍然不知道如何使用 axios 做到这一点.无论我尝试什么, axios 在 Firebase 云函数中都不起作用.这就是我所做的:

I've finally able to do this using the request-promise node package but still no idea about how to do it with axios. As no matter what I try, axios doesn't work in Firebase cloud functions. This is what I did:

npm i --save cors request request-promise

然后这是我运行的代码:gist.github/isaumya/0081a9318e4f7723e0123f4def744a0e

Then this is the code I run: gist.github/isaumya/0081a9318e4f7723e0123f4def744a0e

也许它会帮助某人.如果有人知道如何使用 Axios 请在下面回答.

Maybe it will help someone. If anyone knows how to do it with Axios please answer below.

推荐答案

我把 data.ip 改成了 response.data.ip 并添加了 return 在两个 res.status(... 行之前,当我尝试使用 Axios 时,部署的云功能对我有用.

I changed data.ip to response.data.ip and added return before the two res.status(... lines and the deployed cloud function works for me using Axios when I try it.

我的代码是

const functions = require('firebase-functions'); const axios = require('axios'); const cors = require('cors')({ origin: true }); exports.checkIP = functions.https.onRequest((req, res) => { cors(req, res, () => { if (req.method !== "GET") { return res.status(401).json({ message: "Not allowed" }); } return axios.get('api.ipify?format=json') .then(response => { console.log(response.data); return res.status(200).json({ message: response.data.ip }) }) .catch(err => { return res.status(500).json({ error: err }) }) }) });

当我调用该函数时,我会得到一个回复​​,例如{消息":127.168.121.130"}

When I invoke the function I get back a reply like { "message": "127.168.121.130" }

更多推荐

如何从 Firebase Cloud Function 发出 Axios 请求

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

发布评论

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

>www.elefans.com

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