在next js中运行amazon ses代码时出现错误。不知道我错在哪里,请尝试修复我的错误。如果有什么问题,请尽管提。
sendmail.js
这是sendmail.js文件,我得到了错误。
var AWS = require('aws-sdk');
AWS.config.update({ region: process.env.AWS_REGION });
// var mail = '';
function sendMail(Email) {
var result;
// Create sendEmail params
var params = {
Destination: { /* required */
CcAddresses: [
Email,
/* more items */
],
ToAddresses: [
Email,
/* more items */
]
},
Message: { /* required */
Body: { /* required */
Html: {
Charset: "UTF-8",
Data: "HTML_FORMAT_BODY"
},
Text: {
Charset: "UTF-8",
Data: "TEXT_FORMAT_BODY"
}
},
Subject: {
Charset: 'UTF-8',
Data: 'Test email'
}
},
Source: 'abc@gmail.com', /* required */
ReplyToAddresses: [
'abc12@gmail.com',
/* more items */
],
};
// Create the promise and SES service object
var sendPromise = new AWS.SES({ apiVersion: '2010-12-01' }).sendEmail(params).promise();
// Handle promise's fulfilled/rejected states
sendPromise.then(
function (data) {
result = 'Success';
}).catch(
function (err) {
result = 'Failed';
});
}
export default sendMail;
dynamicid.js
这是我写端点代码的动态id .js文件
import { getDataFromSheets } from '../../../libs/sheets';
import sendmail from '../../../libs/ses/sendmail';
export default function handler(req, res) {
var data;
getDataFromSheets()
.then(sheet => {
data = sheet.length
for (var i = 1; i < data; i++) {
sendmail(sheet[i].Email)
}
})
.catch(err => console.log(err))
}
每个端点函数必须通过发送响应(res.send()
,res.json()
,res.end()
等)来结束请求-响应周期。所以解决方案是:
export default function handler(req, res) {
var data;
getDataFromSheets()
.then(sheet => {
data = sheet.length
for (var i = 1; i < data; i++) {
sendmail(sheet[i].Email)
}
res.json({status: 'success', message: 'email has been sent'})
})
.catch(err => {
console.log(err)
res.json({status: 'fail', error: err})
})
}