Mailchimp电子邮件未使用Node.js处理

时间:2018-10-28 01:22:05

标签: javascript jquery node.js mailchimp superagent

我有一个带有“名称”和“电子邮件”地址的简单HTML表单,并且我使用的是mailchimps API。输入名称/电子邮件并提交后,我的网页上的代码中会显示一条成功消息,但是在终端中,Mailchimp要求输入:

{ type: 'http://developer.mailchimp.com/documentation/mailchimp/guides/error-glossary/',
  title: 'Invalid Resource',
  status: 400,
  detail: 'Please provide a valid email address.',
  instance: '65bae057-c3e6-46fc-b5f7-c287e9208d78' }

我正在对HTTP请求使用Superagent,但我认为我的代码可能设置不正确。在过去的教程中,我使用了webpack,但使用它有些困难(即使我成功完成了工作),所以这次我尝试了超级代理。我也用过邮递员。我的代码看起来不错,我想我只是在这里错过了重要的一块?如果有人可以给我任何指示,我将不胜感激! :)

package.json

{
  "name": "workspace",
  "version": "1.0.0",
  "description": "Teamtrics Wanzeru Draft",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "body-parser": "^1.18.3",
    "express": "^4.16.4",
    "jquery": "^3.3.1",
    "request": "^2.88.0",
    "superagent": "^4.0.0-beta.5"
  }
}

app.js

const express = require('express');
const bodyParser = require('body-parser');
const request = require('superagent');
const app = express();

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

// //middleware, static files HTML where form is stored
app.use(express.static(__dirname + "/"));

//connecting cloud i9 host
app.listen(process.env.PORT, process.env.IP, function() {
    console.log("The Server Has Started!");
});

app.post('/', function(req, res) {
    addEmailToMailchimp(req.body.email);
    res.end('SUCCESS!');
});

function addEmailToMailchimp(email) {
    var request = require("request");

    var options = {
        method: 'POST',
        url: 'https://xxxxxx.api.mailchimp.com/3.0/lists/xxxxxxxx/members',
        headers: {
            'postman-token': 'xxxxxxxx-xxxxxx-xxxx-xxx-xxxxx',
            'cache-control': 'no-cache',
            'content-type': 'application/json',
            authorization: 'Basic xxxxxxxxxx='
        },
        body: { email_address: 'email', status: 'subscribed' },
        json: true
    };

    request(options, function(error, response, body) {
        if (error) throw new Error(error);

        console.log(body);
    });
}

html表单

 <form action = "/" method="post">
                                <div class="form-group">
                                    <input type="name" class="form-control" id="exampleInputName" aria-describedby="emailHelp" placeholder="Name">
                                </div>
                                <div class="form-group">
                                    <input type="email" class="form-control" id="exampleInputEmail" placeholder="Email">
                                </div>
                                <button type="submit" class="btn" style="background-color: #D34ED5; color:#fff; margin-bottom: 70px; width: 190px;">Sign up</button>


                       </form>

0 个答案:

没有答案