The “Velo Tutorial: Sending an Email on Form Submission” seems to contain an error in the sense the provided raw code for section " //email.jsw " throws the error without altering anything " Error parsing web-module ‘backend/email.jsw’: Cannot use keyword ‘await’ outside an async function ".
Even if I manually fix it by adding async code (note it’s “//” for the purpose of this forum post) it will only return “undefined”.
I’m assuming either the backend processing has changed and this code isn’t updated properly or something in these straight-forward instructions aren’t clear. Bear in mind, I even used SendGrid, as-per the highlighted service so this isn’t 3rd-party API related. I’ve trimmed it down to the bear-bones for testing purposes and simply doesn’t work.
Can someone please help or assist?
Backend
//email.jsw (file)
import {sendWithService} from "backend/sendGrid";
import wixSecretsBackend from "wix-secrets-backend";
export function sendEmail(subject, body) {
//(async () => {
const sendGridSecret = JSON.parse(await wixSecretsBackend.getSecret("sendGridSecret"));
const key = sendGridSecret.key;
const sender = sendGridSecret.senderEmail;
const recipient = "myRealEmailGoesHere@gmail.com";
return sendWithService(key, sender, recipient, subject, body);
//})();
}
------------------------------------------------------------------------------------------------------------------------
//sendGrid.js (file)
import {fetch} from "wix-fetch";
export function sendWithService(key, sender, recipient, subject, body) {
const url = "https://api.sendgrid.com/api/mail.send.json";
const headers = {
"Authorization": "Bearer " + key,
"Content-Type": "application/x-www-form-urlencoded"
};
const data = "from=${sender}&to=${recipient}&subject=${subject}&text=${body}";
const request = {
"method": "post",
"headers": headers,
"body": data
};
return fetch(url, request)
.then(response => response.json());
}
OnPage code
import {sendEmail} from 'backend/email';
//BUTTON PRESS TO TEST EMAILER
export function button78_click(event) {
sendFormData()
}
function sendFormData() {
const subject = "New GameSave submittted from Someone";
const body = "GameSave was Uploaded...";
sendEmail(subject, body)
.then(response => console.log(response));
console.log("SendForm Was Called")
}
Thanks : )