简体   繁体   中英

Adding subscriber to mailchimp 3.0 with node https

This is my app.js file:

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.email;


  var data = {
    members: [{
      "email_address": email,
      "status": "subscribed",
      "merge_fields": {
        "FNAME": firstName,
        "LNAME": lastName
      }
    }]
  };

  var jsondata = JSON.stringify(data);

  const url = `https://us8.api.mailchimp.com/3.0/lists/<list id>`; 

  const options = {
    method: "POST",
    auth: "jay:<my API key>"
  };

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

  });

  request.write(jsondata);
  request.end();


});

app.listen(3000, function(){
  console.log("You are now live");
});

When my form submits I get an error status 400 mentioned below:

{
  type: 'http://developer.mailchimp.com/documentation/mailchimp/guides/error-glossary/',
  title: 'Invalid Resource',
  status: 400,
  detail: "The resource submitted could not be validated. For 
field-specific details, see the 'errors' array.",
  instance: '25fa1c2d-0740-4e45-9c9c-43c51cca5862',
  errors: [
    {
      field: 'email_address',
      message: 'This value should not be blank.'
    }
  ]
}
{
  type: 'http://developer.mailchimp.com/documentation/mailchimp/guides/error-glossary/',
  title: 'Invalid Resource',
  status: 400,
  detail: "The resource submitted could not be validated. For 
field-specific details, see the 'errors' array.",
  instance: 'dc94871f-2eba-417a-ba0f-b913de939b64',
  errors: [
    {
      field: 'email_address',
      message: 'This value should not be blank.'
    }
  ]
}

I thought my form did not submit the email address but when I console logged the value, it returns what the user entered. I read the docs and I have great difficulty understanding what the error means... It will be a great help if you could solve this.

Thank you.

This worked.

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.email;


  var data = {
    members:[ {
      email_address: email,
      status: "subscribed",
      merge_fields: {
        FNAME: firstName,
        LNAME: lastName
      }
    }]
  };

  var jsondata = JSON.stringify(data);

  const url = `https://us8.api.mailchimp.com/3.0/lists/de7bf306b4`; 

  const options = {
    method: "POST",
    body: data,
    auth: "#######"
  };


  const request = https.request(url, options, function(response){

    response.on("data", function (data) {
      console.log(JSON.parse(data));
    });

    if(response.statusCode === 200){
      res.sendFile(__dirname + "/success.html");
    } else {
      res.sendFile(__dirname+"/failure.html");
    }



  });

  request.write(jsondata);
  request.end();
  


});

app.post("/failure", function(req,res){
  res.redirect("/");
});

app.listen(process.env.PORT || 3000, function(){
  console.log("You are now live");
});

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