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!