使用节点 https 将订阅者添加到 mailchimp 3.0

Adding subscriber to mailchimp 3.0 with node https

这是我的 app.js 文件:

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");
});

当我提交表单时,我收到下面提到的错误状态 400:

{
  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.'
    }
  ]
}

我以为我的表单没有提交电子邮件地址,但当我在控制台记录该值时,它 returns 是用户输入的内容。我阅读了文档,但很难理解错误的含义……如果您能解决这个问题,那将是一个很大的帮助。

谢谢。

这有效。

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");
});