wixdata create duplicate entry when insert

Hi! I’ve a problem when i’m trying to insert rows in one collection with wix code. It is creating duplicate rows for 1 insert and i dont understand why.

Here the code:

export function button1_click(event) {
 var error = 0;

 if($w("#input1").value === "" || $w("#input2").value === "" ||                 
     $w("#input3").value === ""|| !$w("#input1").valid || 
     !$w("#input2").valid || !$w("#input3").valid){
        $w("#box5").show();
        error = 1;
    } else {
        $w("#box5").hide();
        error = 0;
    }

 if(error === 0){
        session.setItem("name", $w('#input1').value);
        session.setItem("email", $w('#input2').value);
        session.setItem("cellphone", $w('#input3').value);

        wixData.query("users")
        .eq("email", $w("#input2").value)
        .limit(1)
        .find()
        .then((results) => {
             if(results.items.length > 0) {
                let item = results.items[0];
                item.cellphone = $w("#input3").value; 
                item.name = $w("#input1").value;
 
                console.log(item);
                wixData.update("users", item).then((res) => {
                    console.log("actualizado correctamente");
                    wixLocation.to("/brenda-payment");
                }).catch( (err) => {
                    console.log(err.message);
                });
            } else {
                let item = {};
                item.email = $w("#input2").value; 
                item.cellphone = $w("#input3").value; 
                item.name = $w("#input1").value;
                item.subscription_value="200";
                item.creator="brenda";
                wixData.insert("users", item).then(succ => {
                    console.log("Insertado con exito");
                    wixLocation.to("/brenda-payment")
                }).catch( (fail) => {
                    console.log(fail.message);
                });
            }
        }).catch( (err) => {
            let errorMsg = err.message;
            console.log(errorMsg);
        });
    }

Thanks for the help!

It will do.

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.

  • _createdDate: The date the item was added to the collection.

  • _updatedDate: The date the item was modified. When the item is first added, the createdDate and updatedDate are the same.

https://www.wix.com/corvid/reference/wix-data.html#insert
https://www.wix.com/corvid/reference/wix-data.html#update

You might be better off using the Wix Data Save function instead with your code.
https://www.wix.com/corvid/reference/wix-data.html#save

See Ofar (Wix Mod) replies to this previous forum post as he explains it very well here for you.
https://www.wix.com/corvid/forum/community-discussion/please-help-trying-to-insert-data-to-a-database-using-code

Hi @givemeawhisky , thanks for your reply.

I’m change insert method to save method but the result is the same: You can see the follow image of my database collection

I see that when I click in #button1 execute the “onclick” method twice. I dont known why…

Okey i’m solve the solution but i don’t how…

I create new buttons and paste the same code and this solve the problem.

Thanks for the help!