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

查看:66
本文介绍了如何从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
            })
        })

    })
})

我也用Google搜索了很多有关如何将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.

任何人都可以帮忙吗?

附言::我已经在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,并在两条res.status(...行之前添加了return,并且当我使用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天全站免登陆