如何从 Firebase Cloud Function 发出 Axios 请求 [英] How to do Axios request from Firebase Cloud Function

查看:17
本文介绍了如何从 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('https://api.ipify.org?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

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

Then this is the code I run: https://gist.github.com/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('https://api.ipify.org?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 请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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