Firebase Cloud Functions 在调用外部 API 时抛出 DNS 错误

标签 firebase google-cloud-functions

<分区>

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

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

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

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/

如果您想使用第三方 API,则必须转到付费层。

关于Firebase Cloud Functions 在调用外部 API 时抛出 DNS 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48680257/

相关文章:

firebase - 无法使用 gcloud 运行 Firebase 测试实验室测试,并且服务帐户 : 403, 没有 storage.objects.create

javascript - 云函数-查看 data() 对象的所有属性?

google-cloud-platform - GCP 云函数 - 内存消耗

javascript - Cloud Functions for Firebase 在 CORS 预检请求上触发功能

firebase - 在 Cloud Functions for Firebase 中访问数据库数据

多次调用 Firebase 云函数

使用 X-Frame-Options 的 firebase 托管 iframe 错误

javascript - 在 Firebase 函数中按 documentId 查询

firebase - 对象数据类型的 Firestore 规则

node.js - 将 Google People API 与 Cloud Functions for Firebase 结合使用