首页 > 解决方案 > 在 Firebase 云功能中出现“getaddrinfo ENOTFOUND”错误

问题描述

我在 Firebase 云函数中编写了一个函数来向 Onesignal API 发送 HTTP Post。我首先在 Postman 中测试了我的请求,并确保正文和标题正确且有效 - 它成功了。然后我继续使用request npm package创建一个发布请求。这是我到目前为止所拥有的

exports.addMessage = functions.https.onRequest((req, res) => {

const Requestbody = {
    included_segments: ['Subscribed Users'],
    app_id: 'XXXXXXXXXX',
    contents: { 'en': 'Test Notification Body' },
    headings: { 'en': 'Test Title' }
}
httpRequest.post({
    url: 'https://onesignal.com/api/v1/notifications',
    headers: {
        'Authorization': 'Some Token...',
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(Requestbody)
},
    function (error, response, body) {
        if (error) { return res.status(500).send('Failed - ' + JSON.stringify(error)); }
        console.log('Onesignal Response: ' + JSON.stringify(response));
        return res.status(200).send('Success');
    });
}

每次我调用此云函数时,request.post 调用都会返回以下错误: getaddrinfo ENOTFOUND onesignal.com onesignal.com:443

但我提供的那个 URL 正是我在 Post Man 中用来测试的那个。

标签: node.jshttp-postgoogle-cloud-functions

解决方案


正如 Doug Stevenson 和 LundinCast 所指出的,我的 Firebase 计划不支持对外部主机的调用。我需要升级到至少Flame支持它的计划。


推荐阅读