I have completed a form that when submitted will send an email with the information and a link to the document uploaded. The issue starts when the user chooses not to upload a file.
I get the error of “save operation failed: TypeError: Cannot read property ‘match’ of undefined”
What do I need to add to make it so if the user chooses not to add a file to proceed anyway?
Hi,
In order to have the option to save data in both scenarios, you need a bit code. Instead of connecting the use inputs and the upload button to the collection using the editor, you should create an onClick event with the following code:
export async function submit_click(event, $w) {
//creating an object with all the information you would like to save
const toInsert = {hello: 'world'};
//checking if the user uploaded a file
if ($w('#uploadButton1').value.length > 0) {
const file = await $w('#uploadButton1').startUpload();
//adding the file uploaded to the object
toInsert.doc = file.url;
}
//inserting the new object to the collection
wixData.insert("collectionName", toInsert)
.then((results) => {
console.log("inserted");
let item = results; //see item below
})
.catch((err) => {
let errorMsg = err;
});
}
I recommend reading the uploadButton and the insert documentations.
The code above is in order to submit the data to the collection. When you use the editor buttons (meaning connecting the upload button to the collection), it doesn’t let you save the record to your collection if you don’t have a file. Note that this function is not related to the other functions written. The other functions are related to sending the email notification and not to saving the information to the collection.
Hi Andrea, I met the same issue than you. When user decided to not attach a file, he does not receive a confirmation message whereas when he adds a file, it works… I just want to make the upload button optional in the request form.