Why does the code advance here without waiting for the file to upload?
function uploadFiles(i) {
if ($w("#uploadButton" + i).value.length > 0) { // visitor chose a file
console.log("Uploading" + i + ": " + $w("#uploadButton" + i).value[0].name + "- Please wait.")
$w("#uploadButton" + i).startUpload()
.then((res) => {
//files.push(res.url);
console.log("Uploaded: " + res);
$w('#uploadButton' + i).buttonLabel = "Uploaded"
return res
})
.catch((uploadError) => {
console.log("File upload error: " + uploadError.errorCode);
console.log(uploadError.errorDescription);
});
} else { // site visitor clicked button but didn't choose a file
console.log("Please choose a file to upload.")
}
}
async function datacollect() {
let toInsert = {
"name": $w('#firstNameTxt').value,
"lastName": $w('#lastNameTxt').value,
"email": $w('#emailTxt').value,
"address": $w('#addressentry').value,
"phone": $w('#phoneNumber').value,
"street": $w('#street').value,
"unit": $w('#unit').value,
"city": $w('#city').value,
"province": $w('#subdivision').value,
"postcode": $w('#postcode').value,
"country": $w('#country').value,
"membershipType": $w('#membershipOption').value,
"language": language,
"gender": $w('#gender').value,
"waiver1": await uploadFiles(1),
"waiver2": await uploadFiles(2),
"waiver3": await uploadFiles(3),
"waiver4": await uploadFiles(4),
"dateOfBirth": $w('#dob').value,
"ltsDescription": $w('#ltsText').value,
"discount": $w('#discount').value,
"bursary": $w('#bursary').value,
"volunteer": $w('#helpTags').value,
"photos": $w('#photoCheckbox').value
}
//return wixData.insert("Registration", toInsert)
return toInsert
}
export function btnSubmit_click(event) {
if ($w('#firstNameTxt').valid && $w('#lastNameTxt').valid && $w('#emailTxt').valid && $w('#phoneNumber').valid && $w('#membershipOption').valid && $w('#city').valid) {
datacollect().then((toInsert) => {
console.log("Inserting: " + toInsert);
wixData.insert("Registration", toInsert).then((res) => {
let item = res;
console.log("uploaded to database", res._id);
})
.catch((err) => {
let errorMsg = err;
console.log("your upload failed");
});
});
}
}
This is the result:
The uploads only complete after the toInsert is called