Hi all,
*Problem:
Please see code below. After submit the form, I got an error:
“Error occurred in one of afterSave callbacks ReferenceError: recipient is not defined”
*Found:
Done some testing and found that there’s a BUG in fetch (url, request) function, parsing string not correctly.
I hard code the email addresses, it worked, mail delivered OK:
//const data = ‘from=${sender}&to=${recipient}&subject=${subject}&text=${body}’;
const data = "from=test@hotmail.com &to=test@hotmail.com & subject=${subject}&text=${body}" ;
And this’s what sent to my email inbox: ${subject} ${body}. See the snapshot.
There’s a bug in fetch (url, request) function.
return fetch (url, request)
.then(response => response.json());
How to fix and REPORT this bug?
Thank you…
------------- EMAIL.JSW --------------
import { sendWithService } from 'backend/ sendGrid ';
export function sendEmail (subject, body) {
const key = “SG.Ma-ae81tSXef6rGsk_QsGw.v5yvoze4”; // my own SendGrid API Key
const sender = “test@hotmail.com”;
const recipient = “test@hotmail.com”; //<< recipient is defined here
return sendWithService (key, sender, recipient, subject, body);
}
--------------- sendGrid .js ------------------
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**
};
//** **A BUG IN THIS FUNCTION,** *incorrect parsing String* ***//
return **fetch** (url, request)
.then(response => response.json());
}
---------- PAGE CODE ----------
import { sendEmail } from ‘backend/email’;
$w. onReady (function () {
$w(“#dataset1”). onAfterSave (sendFormData);
});
function sendFormData () {
const subject = Testing ${$w("#input8").value}
;
const body = Name: ${$w("#input8").value} \rEmail: ${$w("#input10").value} \rPhone: ${$w("#input7").value}
;
// **recipient** is set in sendEmail()
**sendEmail** (subject, body)
.then(response => console.log(response));
}
------------End Code------------
Can someone please help me to fix this problem.
Thank you very much.