0

I am trying to add a new member to a MailChimp list, but I keep getting the following error and can't understand why.

{
  type: 'https://mailchimp.com/developer/marketing/docs/errors/',
  title: 'Invalid Resource',
  status: 400,
  detail: "The resource submitted could not be validated. For field-specific details, see the 'errors' array.",
  instance: '/* secret */',
  errors: [
    {
      field: 'email_address',
      message: 'This value should not be blank.'
    }
  ]
}

Here is the code:

const express = require("express");
const bodyParser = require("body-parser");
const request = require("request");
const https = require("https");

const app = express();

app.use(express.static("public"));

app.use(bodyParser.urlencoded({
  extended: true
}));

app.get("/", function(req, res) {
  res.sendFile(__dirname + "/signup.html")
})

app.post("/", function(req, res) {
  const firstName = req.body.fname;
  const lastName = req.body.lname;
  const email = req.body.mail;
  console.log(firstName);
  console.log(lastName);
  console.log(email);

  const data = {
    members: [{
      email_address: email,
      status: "Subscribed",
      merge_fields: {
        FNAME: firstName,
        LNAME: lastName
      }
    }]
  };
  const jsonData = JSON.stringify(data);
  const url = "https://us5.api.mailchimp.com/3.0/lists/4e5319cda3/members"
  const option = {
    method: "post",
    auth: "/* secret */"
  }

  const request = https.request(url, option, function(response) {
    response.on(
      "data",
      function(data) {
        console.log(JSON.parse(data));
      })
  })

  request.write(jsonData);
  request.end();
});

app.listen(3000, function() {
  console.log("server is working in port 3000");
});
Jorengarenar
  • 2,705
  • 5
  • 23
  • 60
  • you're sending a blank `email_address` - at least, that's what the error tells you - perhaps you're doing something wrong in your `javascript` `web-deployment` `mailchimp` `mailchimp-api-v3.0` or `mailchimp-api-v3` – Bravo Aug 30 '21 at 05:20
  • Please share more details, like the code involved and your attempts to resolve the problem – Nico Haase Aug 30 '21 at 05:22
  • app.post("/", function(req, res) { const firstName = req.body.fname; const lastName = req.body.lname; const email = req.body.mail; console.log(firstName); console.log(lastName); console.log(email); const data = { members: [{ email_address: email, status: "Subscribed", merge_fields: { FNAME: firstName, LNAME: lastName } }] }; – Raj kumar jha Aug 30 '21 at 05:23
  • Please add all clarification **to your question** by editing it – Nico Haase Aug 30 '21 at 05:26

2 Answers2

0

I think this endpoint from Mailchimp accept only one element not a list of members, try to send only one member

this is an example of the body from their documentation website

{"email_address":"","email_type":"","status":"subscribed","merge_fields":{},"interests":{},"language":"","vip":false,"location":{"latitude":0,"longitude":0},"marketing_permissions":[],"ip_signup":"","timestamp_signup":"","ip_opt":"","timestamp_opt":"","tags":[]}

In your case try to change your data const to :

  const data = {
  email_address: email,
  status: "Subscribed",
  merge_fields: {
    FNAME: firstName,
    LNAME: lastName
  }
};
0

Remove /members form ... this

const url = "https://us5.api.mailchimp.com/3.0/lists/4e5319cda3/members"