0

I'm using this code to get a list of members in an audience and put their email adresses in arrays:

var mailchimpMarketing = require("@mailchimp/mailchimp_marketing");

mailchimpMarketing.setConfig({
  apiKey: "MY API KEY",
  server: "MY SERVER",
});

async function getArrayEmailMembersFromMailchimpListID(listID, offset , count){
  const response = await mailchimpMarketing.lists.getListMembersInfo(listID, {offset, count});
  const emailsMailchimp = response.members.map(member => member.email_address);
  console.log(emailsMailchimp)
  return emailsMailchimp;
}

async function getEmailsAccordingSize(listID){
  const response = await mailchimpMarketing.lists.getListMembersInfo(listID)
  offset = 0
  totalItems = await response.total_items
  do { 
    count = 1000
    getArrayEmailMembersFromMailchimpListID(listID, offset, count)
    offset = offset + 1000
  } while (offset < totalItems);
}

getEmailsAccordingSize("MY LIST ID");

My problem is when I'm making this to a very big list of members in an audience it return to me this error:

(node:8014) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: Too Many Requests
(node:8014) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

I'm assuming I can only make 10 calls in a row. So how can I make more ? I have an audience with 37 000 members in it and I have to take all their emails adresses.

I don't understand the batches endpoint that are explain on mailchimp website (https://mailchimp.com/developer/guides/run-async-requests-with-the-batch-endpoint/). Is it possible to do it with batches ? How ?

Samy Rharade
  • 95
  • 1
  • 7
  • 1
    This line `getArrayEmailMembersFromMailchimpListID(listID, offset, count)` inside the loop seems weird; shouldn't you `await` that? And do something with the result? –  Oct 19 '20 at 08:43
  • I don't think so because I did it with an other list that contain only 1058 members and it works very well – Samy Rharade Oct 19 '20 at 08:47
  • Well I put an `await` in front of the line `getArrayEmailMembersFromMailchimpListID(listID, offset, count)` and it actualy work but take a long time to display all email adresses. Thanks for your help ! – Samy Rharade Oct 19 '20 at 08:51

0 Answers0