PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, September 6, 2022

[FIXED] How to submit a form (add a subscriber) by mailchimp-api-v3 in Node.js?

 September 06, 2022     javascript, mailchimp-api-v3.0, node.js     No comments   

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)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing