Same _id after submit form

Hi,
I have a multi-state box and in every state there is a form, build with Wix Forms, but with custom submit buttons that “collect” the inputs from all forms, and at the last form there is a submit button to insert it into the database, using wixData.

When I filled the form and submit, everything worked and the input saved into the table, but when I tried it again I got an error: “an item with _id [XXXXXX-XXX-XXX] already exists”.
The _id is the same _id of the previous insert.

When I reload the page it’s worked, but again, only for the first time.

I checked that the primary key is different of course.

This is the submit function:

export function back2Button_click(event) {
    wixData.insert("ConsultationTable", toInsert)
        .then( (results) => {
             let item = results; 
             resetForm();
        } )
        .catch( (err) => {
              let errorMsg = err;
        } );
}

‘toInsert’ is the object that contains the inputs.
‘resetForm()’ is function to reset the forms (clear textboxes, uncheck checkboxes and so on:

function resetForm() {
    $w('#input2').value = '';
    $w('#input3').value = '';
    $w('#input4').value = '';
    $w('#input12').value = '';
    $w('#input13').value = '';
    $w('#input2').resetValidityIndication();
    $w('#input13').resetValidityIndication();

 const textinputList = $w('TextInput');
        textinputList.forEach(element => {
            element.checked = false;
    });

    $w('#input19').value = '';
    $w('#input20').value = '';
    $w('#input21').value = '';
    $w('#input22').value = '';
    $w('#input23').value = '';

 if (formFactor === "Desktop") {
        $w('#input19').hide();
        $w('#input20').hide();
        $w('#input21').hide();
        $w('#input22').hide();
        $w('#input23').hide();
    } else {
        $w('#input19').collapse();
        $w('#input20').collapse();
        $w('#input21').collapse();
        $w('#input22').collapse();
        $w('#input23').collapse();
    }
 
    $w('#textBox1').value = '';
    $w('#textBox1').collapse();

    $w('#datePicker1').value = null;
}

Thank you!

Hey Matan!

Care to share a URL to your site so we can check it out?

Doron.

Hi Doron,

https://negev19.wixsite.com/mysite-8

Hey Matan!

I believe that the issue is due to the fact that although you are resetting the input elements to their default state, the item and assigned Id remains the same (in your toInsert object).

I suggest providing the id to the inserted object yourself.
You can use the following example from stackoverflow to generate a random UUID with the same structure that the Wix metadata uses and thus prevent the same id from being pushed again and again.

Hope that helps.

Doron.

Hi,

I will provide my own random _id, but I wonder why this does not happen automatically by ‘insert ()’.
According to the ‘insert()’ documentation:
"
The insert() function adds the following properties and values to the item when it adds it to the collection:

  • _id: A unique identifier for the item with collection, if the item does not include one. You can optionally provide your own ID. Once an ID is assigned to an item it cannot be changed.
    "

Thanks.

SOLVED
So it turns out that the problem was that I did not reset the ‘toInsert’ object after submitting the form.
I added to the ‘resetForm()’ function a line for resetting the ‘toInsert’ object and it solved the problem.