Issue
I were using a hack way by jsonp to submit form (add a subscriber) in front end before (because Mailchimp only provide API for back end).
Now I want to switch to back end to use Mailchimp API. I am using a library called mailchimp-api-v3 to simplify the work.
This list only has three fields, which are FNAME
, EMAIL
, and COMPANY
.
const Mailchimp = require('mailchimp-api-v3');
const mailchimp = new Mailchimp(myMailchimpAPI);
mailchimp.post(`/lists/${myListId}/members`, {
FNAME: 'Jack',
EMAIL: 'jack@example.com',
COMPANY: 'Apple'
})
.then(res => console.log(res))
.catch(err => console.log(err));
This gives me error:
[ { field: 'email_address', message: 'This value should not be blank.' } ] }
Solution
I figured out after reading the Mailchimp API document.
I have to add another two fields email_address
and status
which are required. Then move the rest of form fields under merge_fields
.
The correct code is
const Mailchimp = require('mailchimp-api-v3');
const mailchimp = new Mailchimp(myMailchimpAPI);
mailchimp.post(`/lists/${myListId}/members`, {
email_address: 'jack@example.com',
status: 'subscribed',
merge_fields: {
FNAME: 'Jack',
EMAIL: 'jack@example.com',
COMPANY: 'Apple'
}
})
.then(res => console.log(res))
.catch(err => console.log(err));
It was hard for me to find a simple demo online. Hope this helps for future people.
Answered By - Hongbo Miao Answer Checked By - Mildred Charles (PHPFixing Admin)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.