如何通过 Node JS 使用自己的库将联系人添加到 Mailchimp 中的 list/audience

How can I add a contact to a list/audience in Mailchimp using its own library via Node JS

下面的代码是 API 调用,使用 Node JS 和 Mailchimp 库以编程方式将联系人添加到 list/audience。文档位于:https://mailchimp.com/developer/marketing/guides/create-your-first-audience/#add-a-contact-to-an-audience

const listId = "YOUR_LIST_ID";
const subscribingUser = {
 firstName: "Prudence",
 lastName: "McVankab",
 email: "prudence.mcvankab@example.com"
};

async function run() {
const response = await mailchimp.lists.addListMember(listId, {
  email_address: subscribingUser.email,
  status: "subscribed",
  merge_fields: {
    FNAME: subscribingUser.firstName,
    LNAME: subscribingUser.lastName
  }
});

console.log(
  `Successfully added contact as an audience member. The contact's id is ${
    response.id
  }.`
);
}
run();

下面是我在 app.js

中实现代码的方式
app.post("/", function(req, res) {
 const firstName = req.body.firstName;
 const lastName = req.body.lastName;
 const email = req.body.email;

 const apiAudienceName = "Sample Tech Newsletter Subscription";

 const listId = apiAudienceName;
 const subscribingUser = {
  firstName: firstName,
  lastName: lastName,
  email: email
 };

 async function run() {
   const response = await mailchimp.lists.addListMember(listId, {
     email_address: subscribingUser.email,
     status: "subscribed",
     merge_fields: {
       FNAME: subscribingUser.firstName,
       LNAME: subscribingUser.lastName
     }
   });

   console.log(`Successfully added contact as an audience member. The contact's id is ${response.id}.`);

 }

 run();
});

对我来说,我确实按照 Mailchimp 服务器的要求添加到我创建的列表中,但是这段代码抛出了一个错误 “Unhandled promise rejection”。当然,我已经尝试进行研究,但作为这门语言的初学者,我并不太了解实现这一目标需要什么。如果有人可以纠正我或告诉我代码中的错误,我将不胜感激。非常感谢!

注意:我能够通过使用 HTTP 请求模块来完成这项工作。但是这次,我想学习如何遵循文档并使用他们给定的代码和库。对我来说,似乎我这样做了,但似乎没有用。

通过我所在的课程社区的研究和帮助,我发现了我的代码中的问题。我希望这可以帮助 dealing/will 可能处理相同问题的人。

const apiAudienceName = "Sample Tech Newsletter Subscription";

const listId = apiAudienceName;

正如您在上面的代码中看到的,我正在将我在 Mailchimp 网站上创建的 "list/audience 名称" 输入到 listID 多变的。问题是 Mailchimp API 期望 listID 而不是 Mailchimp API.

中的列表名称

解决此问题的答案是从您在 Mailchimp 网站上的帐户获取 list/audience ID 并将其用作输入。 list/audience ID 是由 Mailchimp 服务器提供的字母数字字符。

如果您需要有关查找 list/audience ID 的详细指南,请查看此 link

这是解决此问题的示例代码:

app.post("/", function(req, res) {
 const firstName = req.body.firstName;
 const lastName = req.body.lastName;
 const email = req.body.email;

 const listId = "1a2b3c4d5e777";
 const subscribingUser = {
  firstName: firstName,
  lastName: lastName,
  email: email
 };

 async function run() {
  const response = await mailchimp.lists.addListMember(listId, {
   email_address: subscribingUser.email,
   status: "subscribed",
   merge_fields: {
     FNAME: subscribingUser.firstName,
     LNAME: subscribingUser.lastName
   }
 });

 console.log(`Successfully added contact as an audience member. The contact's id is ${response.id}.`);

 }

 run();
});

您尝试过 subsribi 了吗?它有一个简单的 API,您可以使用 JavaScript、

将订阅者添加到您的列表
  • 示例:

    常量数据 = { 电子邮件:“sarah@mail.com”,//*必填 用户名:“莎拉”,//可选 listId:'[LIST_ID]',

    }
      const url = 'https://api.subscribi.io/v1/subscribers/add'
      fetch(url,{
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
          'secret_key':'MY_SECRET_KEY' //  Go the the settings and generate a key
        }, 
        body: JSON.stringify(data)
      }).then(response =>{
        console.log(response)
        /*
        {
          "data": {
              "name": "sarah",
              "email": "sarah@mail.com",
              "createdAt": "2020-09-25T19:41:07.350Z",
              "subscribed": true
          },
          "ok": true,
          "status": "success"
      }
    
        */
      }).catch(error=>{
        console.error(error)
      })