Search code examples
javascriptnode.jsmailchimpunhandledunhandled-promise-rejection

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


The code below is the API call to add a contact to a list/audience programmatically using Node JS and the Mailchimp library. Documentation is found on: 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: "[email protected]"
};

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();

Here's how I implemented the code in my 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();
});

To me, I did the exact requirement of the Mailchimp server to add to my created list but this code is throwing an error saying "Unhandled promise rejection". I've tried to do my research of course but as a beginner in this language, I don't really quite grasp what is needed for me to make this happen. If someone could correct me or show me the error in my code, I would appreciate it. Thank you very much!

NOTE: I was able to make this work by using HTTP request module. But for this time, I wanted to learn how to follow a documentation and use their given code and library. To me, it seems that I did that but it doesn't seem to work.


Solution

  • Through research and help from the course community that I am in, I found the problem in my code. I hope this helps someone who's dealing/will potentially deal with the same problem.

    const apiAudienceName = "Sample Tech Newsletter Subscription";
    
    const listId = apiAudienceName;
    

    As you can see in the above code, I am inputting the "list/audience name" that I have created in the Mailchimp website into the listID variable. The problem is that the Mailchimp API is expecting the listID and not the name of the list that you have in the Mailchimp API.

    The answer that solved this problem was getting the list/audience ID from your account in the Mailchimp website and using it as an input. The list/audience ID is an alphanumeric characters provided by the Mailchimp server.

    If you want a detailed guide on finding your list/audience ID, check this link.

    Here's a sample code that solved this problem:

    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();
    });