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

查看:25
本文介绍了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天全站免登陆