我正在研究next.js项目,并且在本地运行我的项目时,我能够将电子邮件插入表单并通过其API成功地将其发布到MailChimp。在生产环境中,相同的代码不起作用,但我已经确认生产环境具有以下功能:
- 访问并更正MailChimp API密钥下一个API路由
- Api路线
{URL}/subscribe
返回200
问题似乎出在API路由被调用并且随后的API调用发生之后。我迷路了,很乐意您的见识。
pages / api / subscribe.js
const axios = require('axios');
const LIST_ID = 'b29d6f1e61'; // MailChimp Audience
const key = `Basic ${Buffer.from(`apikey:${process.env.MAILCHIMP_API_KEY}`).toString('base64')}`;
const subscribe = ( email ) => {
axios({
method: 'POST',url: `https://us18.api.mailchimp.com/3.0/lists/${LIST_ID}/members/`,headers: {Authorization: key},data: { email_address: `${email}`,status: "subscribed" } })
.then(response => {
console.log(response);
return response
}).then(data => {
console.log('Data Rec\'d:',data);
}).catch(error => {
console.log('Subscribe error:',error);
// reject('Something went wrong')
});
};
module.exports = async (req,res) => {
if (req.method === 'POST') {
res.setHeader('Content-Type','application/json');
res.statusCode = 200;
res.send(req);
await subscribe(req.body);
res.end()
} else {
res.setHeader('Content-Type','application/json');
res.statusCode = 200;
res.end(JSON.stringify({name: 'GET received'}))
}
};