Firebase Cloud Functions调用外部API时会引发DNS错误 [英] Firebase Cloud Functions throws a DNS error when calling an external API

查看:94
本文介绍了Firebase Cloud Functions调用外部API时会引发DNS错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试调用CoinMarketCap的公共API,但是它总是失败,并出现以下错误:

I am trying to call CoinMarketCap's public API but it always fails with following error:

error occured Error: getaddrinfo ENOTFOUND api.coinmarketcap.com api.coinmarketcap.com:443

当我从浏览器中调用URL时,它会立即返回结果.代码很简单:

When I call the URL from the browser it returns the result instantly. The code is pretty simple:

const functions = require('firebase-functions');
const axios = require('axios');
exports.getBtcPrice = functions.https.onRequest((req, res) => {
    axios.get('https://api.coinmarketcap.com/v1/ticker/bitcoin')
        .then( (response) => {
            console.log(response);
            res.send("data received");
        })
        .catch((error) => {
            console.log(error);
            res.send("error occured "+ error)
        });
});

推荐答案

如果您是免费计划,则具有firebase功能的出站网络仅适用于google服务.他们提到这是定价页面 https://firebase.google.com/pricing/

If you are on the free plan outbound networking with firebase functions only works with google services. They mention this is the cloud function section on the pricing page https://firebase.google.com/pricing/

如果要使用第三方API,则必须转到付费层.

You have to move to a paid tier if you want to use third-party APIs.

这篇关于Firebase Cloud Functions调用外部API时会引发DNS错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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