【问题标题】:Cloud Functions for Firebase Error: getaddrinfo ENOTFOUNDFirebase 错误的云函数:getaddrinfo ENOTFOUND
【发布时间】:2017-09-06 23:55:39
【问题描述】:

我有请求功能并尝试为 Firebase 集成宽度云功能。

var http = require("https");

    function addUserToMailchimpList(email) {
    var options = { method: 'POST',
      url: 'https://usxx.api.mailchimp.com/3.0/lists/xxxxxxx/members/',
      headers: 
       { 'content-type': 'application/json',
         'user' : 'anystring:xxxxxxxxxxxxxxxxxxxxxxx'
       },

      body: 
       { email_address: email,
         status: 'subscribed',
       },
      json: true };

    request(options, function (error, response, body) {
      if (error) throw new Error(error);
      console.log(body);
      console.log(error);
      console.loh(response);
    });

}

然后我尝试部署宽度这个功能

exports.sendWelcomeEmail = functions.auth.user().onCreate(event => {
  const user = event.data; // The Firebase user.
  const email = user.email; // The email of the user.
  const displayName = user.displayName; // The display name of the user.

  return addUserToMailchimpList(email);
});

但在 firebase 函数日志中出现错误:

Error: Error: getaddrinfo ENOTFOUND usxx.api.mailchimp.com usxx.api.mailchimp.com:443

我做错了什么?不明白..?

我尝试使用的其他方法但同样的错误

function addUserToMailchimpList(email) {

  var options = {
    "method": "POST",
    "hostname": "usxx.api.mailchimp.com",
    "port": null,
    "path": "/3.0/lists/xxxxx/members/",
    "headers": {
      "content-type": "application/json",
      "user": "anystring:xxxxxxxxxx",
    }
  };

  var req = http.request(options, function (res) {
    var chunks = [];

    res.on("data", function (chunk) {
      chunks.push(chunk);
    });

    res.on("end", function () {
      var body = Buffer.concat(chunks);
      console.log(body.toString());
    });
  });

  req.write(JSON.stringify({ email_address: email,
    status: 'subscribed',
    merge_fields: { FNAME: 'xxx', LNAME: 'xxx' } }));
  req.end();

}

【问题讨论】:

  • usxx.api.mailchimp.com 无法解析为 IP 号码。主机名无效、您的 DNS 不知道、某些防病毒/防火墙软件阻止查找等。
  • 我是从 localhost 部署的,但功能是从 firebase 执行的
  • 也许您不允许从 Firebase 创建网络连接? This blogpost 建议,为了发出外部 API 请求,您需要启用计费。
  • 是的,这似乎是我收到错误的原因。谢谢
  • 你可以添加这个作为答案,所以我会接受。

标签: javascript node.js firebase google-cloud-functions


【解决方案1】:

This blogpost 建议您在从 Firebase Cloud Function 执行 API 请求之前,需要先启用结算功能。

【讨论】:

    猜你喜欢
    • 2019-03-05
    相关资源
    最近更新 更多