简体   繁体   中英

Node + Mailchimp NPM: How to add a subscriber to a list and include their first and last name?

Mailchimp is almost a perfect company, except their Node API documentation is non-existent. How can I add a subscriber to my new list and include their first name and last name? The code below successfully adds the subscriber, but first and last names are not being added.

var MCapi = require('mailchimp-api');
MC        = new MCapi.Mailchimp('***********************-us3');

addUserToMailchimp = function(user, callback) {
  var merge_vars = [
      { EMAIL: user.email }, 
      { LNAME: user.name.substring(user.name.lastIndexOf(" ")+1) },
      { FNAME: user.name.split(' ')[0] }
  ];

  MC.lists.subscribe({id: '1af87a08af', email:{email: user.email}, merge_vars: merge_vars, double_optin: false }, function(data) {
      console.log(data);
  }, function(error) {
      console.log(error);
  });
}; // addUserToMailchimp

The supplied merge variables should be passed as a single object, not an array of objects. Please see my example below:

var mcReq = {
    id: 'mail-chimp-list-id',
    email: { email: 'subscriber-email-address' },
    merge_vars: {
        EMAIL: 'subscriber-email-address',
        FNAME: 'subscriber-first-name',
        LNAME: 'subscriber-last-name'
    }
};

// submit subscription request to mail chimp
mc.lists.subscribe(mcReq, function(data) {
    console.log(data);
}, function(error) {
    console.log(error);
});

It looks like you supplied your actual mail chimp API key in your question. If so, you should remove it immediately.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM