Hi All
I’m stuck … any suggestions would be greatly appreciated.
I’m trying to send an email to a list of addresses using SendGrid after performing a database update. My problem is how to call the emailing function, repeatedly, only on successfully saving to the database.
This page should update a table record and then inform on those changes via email.
import {sendEmail} from 'backend/email';
import wixData from 'wix-data';
$w.onReady(function () {
wixData.query("players")
.limit(1000)
.distinct("email")
.then((results) => {
if (results.items.length > 0) {
let items = results.items;
let totalCount = results.totalCount;
console.log("*** The total count is ", totalCount);
for (let i = 0; i < totalCount; i++) {
console.log("*** The ", i, "item is ", items[i]);
}
} else {
// handle case where no matching items found
console.log("*** There were no emails returned.")
}
})
.catch((error) => {
let errorMsg = error.message;
let code = error.code;
});
const thisRecipient = "jc@mediaspark.com"
$w("#dynamicDataset").onAfterSave(sendFormData);
function sendFormData() {
const recipient = thisRecipient;
const subject = `theSimon - Game #${$w('#textGameNumber').text}`;
const body = `${$w('#textDivision').text}
\r ${$w('#textTeam1').text} : ${$w("#input1").value}
\r ${$w('#textTeam2').text} : ${$w("#input2").value}`;
sendEmail(subject, body, recipient)
.then(response => console.log(response));
}
});