Hi,
I’m new to Wix Code and, thanks to the tutorials and online docs, have had success at getting a form to send an email and also doing a DB query. I’m stuck trying to figure out how to get a function to properly return a value. I’ve spent way too much time on this already, but am hoping someone might be able to help a newb.
The first line in the ‘sendFormData’ function (which I’ve modified from the tutorial) doesn’t return a value from the checkDB function. The checkDB function is working, but not returning a value as the console.log message shows ‘undefined’. Thanks for any help you’re able to give.
Bill
Here’s my code:
import {sendEmail} from ‘backend/email’;
import {wixData} from ‘wix-data’;
$w.onReady(function () {
$w(“#dataset1”).onAfterSave(sendFormData);
});
function checkDB(email){
console.log(“Line 13”, email);
let subjectLine;
let EmailFind = email;
wixData.query(“Contact_emails”)
.contains(“contact_1”, EmailFind)
.find()
.then((results) => {
console.log(“Line 18:”, results);
if (results.items.length === 0) {
subjectLine = “**";
console.log(subjectLine, “Line 30: Not in Database”);
} else {
subjectLine = "”;
console.log(subjectLine, “Line 34: Found in Database”);
}
})
return subjectLine;
}
function sendFormData() {
const subLine = checkDB($w(‘#input5’).value); // runs function, but no value returned
//console.log("Line 32: ", subLine);
const email = ${$w("#input5").value}
;
const subject = ${subLine} ${$w("#dropdown2").value} Report from ${$w("#input5").value}
;
const body = Parent/Guardian: ${$w("#input1").value} ${$w("#input2").value} \rStudent Name: ${$w("#input3").value} ${$w("#input4").value} \rParent Email: ${email}
;
sendEmail(email, subject, body)
.then(response => console.log(response));
}
you need to do the return through each layer
function checkDB(email){
console.log("Line 13", email);
let subjectLine;
let EmailFind = email;
return wixData.query("Contact_emails") //return contents to checkDB()
.contains("contact_1", EmailFind)
.find()
.then((results) => {
if (results.items.length === 0) {
return "***"; //return contents to wixData.query
} else {
return "*"; //return contents to wixData.query
}
})
}
Thanks very much Ethan for the help. I tried the additions to the code, but still no luck so I added async and await and that brought success with the return values, but then nothing after the first line in the sendFormData function executed. Any other suggestions? I’ve been reading through documentation, but haven’t found any examples that are similar.
async function checkDB(email){
console.log("Line 13", email);
let subjectLine;
let EmailFind = email;
return wixData.query("Contact_emails") //return contents to checkDB()
.contains("contact_1", EmailFind)
.find()
.then((results) => {
if (results.items.length === 0) {
return "***"; //return contents to wixData.query
} else {
return "*"; //return contents to wixData.query
}
})
}
async function sendFormData() {
const subLine = await checkDB($w('#input5').value); // runs function, but no value returned
console.log("Line 26: ", subLine);
const email = `${$w("#input5").value}`;
const subject = ` ${subLine} ${$w("#dropdown2").value} Report from ${$w("#input5").value}`;
const body = `Parent/Guardian: ${$w("#input1").value} ${$w("#input2").value}
\rStudent Name: ${$w("#input3").value} ${$w("#input4").value}
\rParent Email: ${email}`;
sendEmail(email, subject, body)
.then(response => console.log(response));
}
@kingsland Hi Bill!
I believe that the issue is not in the function but in how it is being called.
You are calling the function ‘sendFromData()’ in the onReady function of the page meaning that it is happening as soon as the page is done loading.
Usually, on load, you’re yet to fill the input fields so unless you are populating the fields on load from some Collection or something, you’ll get a broken function since there’s no input or value to those 5 input fields and dropdown.
If that solves it, Great - happy to help.
If not, please share a link to your site so I can inspect it and provide you with a solution.
Only authorized personnel can inspect your site so no worries!
Doron.
Hi Doron, thanks for the help. I have had problems with the functions executing as the page loads, but I think that’s working in this version. Here are the lines I have at the top of the form:
$w.onReady(function () {
$w("#dataset1").onAfterSave(sendFormData);
});
Do those lines cause execution of the sendFormData to wait for the submit button? At any rate, I’m still getting “undefined” although in the current version the return value (either “" or "**”) is working. The problem is that the rest of the form data is not being read.
Here’s a link to the form:
https://bkingsland.wixsite.com/acrss/copy-of-cisva-dates