I am trying to write a simple lambda function which first inserts a record into a db and then sends a templated email via SES. I am able to either do the insert or send the email, but I can’t seem to get both done in the same lambda function.
This code correctly inserts the record and completes without error, but the email is never sent:
async function getValues(db_connection, sql) {
await new Promise((resolve, reject) => {
db_connection.query(sql, function (err, result) {
if (err) {
response = {
statusCode: 500,
body: { message: "Database Connection Failed", error: err },
};
console.error("getValues err: " + err);
resolve();
} else {
console.info("getValues result: " + JSON.stringify(result));
res = result;
sendEmail("[email protected]", "someTokenHere");
resolve();
}
});
});
}
async function sendEmail(toEmailAddr, token) {
const input = {
Source: FROM_EMAIL,
Destination: {
ToAddresses: [toEmailAddr],
},
Template: TEMPLATE_NAME, // required
TemplateData: JSON.stringify({
verifyHref: token,
}), // required
}; // end of input
const command = new SendTemplatedEmailCommand(inp`your text`ut);
try {
let returnObj = await client.send(command);
console.log("EMAIL SENT: " + returnObj);
return returnObj;
} catch (caught) {
if (caught instanceof Error && caught.name === "MessageRejected") {
const messageRejectedError = caught;
console.log(messageRejectedError);
return messageRejectedError;
}
console.log("GOT ERROR: " + caught);
throw caught;
}
}
When I try to add “async” before the call to sendEmail, I get a syntax error. Any help will be greatly appreciated.
cynzu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.