toInsert creates duplicated entry

Hey,

I am using the following code to insert data to the collection.
It works but creates duplicated data entry.

Any idea?

export function submit_click(event) {
let toInsert = {
“name”: $w(‘#FirstName’).value,
“lastName”: $w(‘#LastName’).value,
“email”: $w(‘#email’).value,
“challenge”: $w(‘#challenge’).value,
“imun1Date”: date,
“imun2Date”: date,
“imun3Date”: date,
“imun4Date”: date,
“imun5Date”: date,
“imun6Date”: date,
“imun7Date”: date,
“imun8Date”: date
};
wixData.insert(“Trainees2”, toInsert)
$w(‘#SorveyStrip’).collapse()
$w(‘#infoStrip’).expand()
}

If it is for a submit button then just use save instead of insert.

Plus, is this a submit button that is already linked to save it into a dataset already, as if you’ve done this then that might explain why you are getting duplicate entries.

There are two ways to go about creating a submit button:
.

  1. Add a regular button and use onClick event handler.
    From there you can submit the form bound to a dataset using
$('#dataset1').save()

(replace #dataset1 with actual ID of your dataset)

You can add event handlers by clicking + in the properties panel of the element.

  1. Submit the form using regular button bound to save action and perform additional actions when form is submitted in onAfterSave handler on a DataSet.
    https://www.wix.com/corvid/reference/wix-dataset.Dataset.html#onAfterSave

Also note that when toInsert runs, you can also use beforeInsert and afterInsert if you wanted to.
See examples below in api section for making first and last names always begin with capital letters or to create a full name input from combining first and last names etc.
https://www.wix.com/corvid/reference/wix-data.Hooks.html#beforeInsert
https://www.wix.com/corvid/reference/wix-data.Hooks.html#afterInsert

2 Likes

Thanks.
Solved by deleting the button and adding a new one with a different name