Problem saving data to database

Hi everyone,
I have a problem saving data to database using query.
I’m using a custom form and all the elements not connected to database

I have main database called cards
and child database called cardsdetales

on the page load if the page status “add” is make the input text empty for user entry and if the page status “edit” it fills the input text with the data form the database using query.

after the user finished he press save button which includes save data to main and child database. here is the problem. some times the data saved fine but some times delete the data from the database . it takes days days from me to find the problem.

This is the onready function.

$w.onReady(async function () {
$w(‘#captchatext’).text = (Math.floor((Math.random() * 20000) + 1)).toString();
const { loginEmail } = await currentMember.getMember();
const newId = await uuid()
Email = loginEmail;
language();
$w(‘#reqCardname’).hide();
$w(‘#reqFirstname’).hide();
$w(‘#reqLastname’).hide();
let query1 = wixLocation.query;
taskName = query1.task;
if (taskName == ‘add’) {
primaryId = newId;
$w(‘#languageDropdown’).value = “1”;
}
else if (taskName == ‘edit’) {
primaryId = query1.key;

id = await wixData.query(‘cards’)
.eq(‘primaryId’, primaryId)
.find()
.then((result) => {
if (result.items.length > 0) {
return result.items[0]._id;
} else {
console.warn(‘No results were found’)
}
})
wixData.query(“cards”)
.eq(“primaryId”, primaryId)
.find()
.then((results) => {
if(results.items.length > 0) {
$w(‘#cardName’).value = results.items[0].cardname;
$w(‘#firstName’).value = results.items[0].firstname;
$w(‘#middleName’).value = results.items[0].middlename;
$w(‘#lastName’).value = results.items[0].lastname;
$w(‘#jobTitle’).value = results.items[0].jobtitle;
$w(‘#department’).value = results.items[0].department;
$w(‘#company’).value = results.items[0].company;
$w(‘#image1’).src = results.items[0].image;
$w(‘#languageDropdown’).value = String(results.items[0].cardlanguage);
}
});
wixData.query(“cardsdetailes”)
.eq(“foreignkey”, id)
.find()
.then((results) => {

    if(results.totalCount > 0) { 
      let j = results.totalCount; 
      for (var i=0;i<j;i++){ 
      if (results.items[i].sortkey == 1) { 
        $w('#phoneInput').value = results.items[i].socialid; 

      } 
      if (results.items[i].sortkey == 2) { 
        $w('#emailInput').value = results.items[i].socialid; 
      } 
      if (results.items[i].sortkey == 3) { 
        $w('#linkInput').value = results.items[i].socialid; 
      } 
      if (results.items[i].sortkey == 4) { 
        $w('#websiteInput').value = results.items[i].socialid; 
      } 
      if (results.items[i].sortkey == 5) { 
        $w('#twitterInput').value = results.items[i].socialid; 
      } 
      if (results.items[i].sortkey == 6) { 
        $w('#snapchatInput').value = results.items[i].socialid; 
      } 
      if (results.items[i].sortkey == 7) { 
        $w('#instagramInput').value = results.items[i].socialid; 
      } 
      if (results.items[i].sortkey == 8) { 
        $w('#tiktokInput').value = results.items[i].socialid; 
      } 
      if (results.items[i].sortkey == 9) { 
        $w('#linkdinInput').value = results.items[i].socialid; 
      } 
      if (results.items[i].sortkey == 10) { 
        $w('#whatsappInput').value = results.items[i].socialid; 
      } 
      if (results.items[i].sortkey == 11) { 
        $w('#facebookInput').value = results.items[i].socialid; 
      } 
      if (results.items[i].sortkey == 12) { 
        $w('#youtubeInput').value = results.items[i].socialid; 
      } 
    } 
}  

});

}
});

This is the save button function
export async function saveButton_click(event) {

if ($w(‘#captchaInput’).value !== $w(‘#captchatext’).text) {
$w(‘#captchaInput’).style.borderColor = “red”;
}
else {
$w(‘#captchaInput’).style.borderColor = “black”;
//** ADD **/
if (taskName == “add”){
if ($w(‘#cardName’).value == “”) {
$w(‘#reqCardname’).show();
setTimeout(function() {
$w(‘#reqCardname’).hide();
}, 3000)
}
else if ($w(‘#firstName’).value == “”) {
$w(‘#reqFirstname’).show();
setTimeout(function() {
$w(‘#reqFirstname’).hide();
}, 3000)
}
else if ($w(‘#lastName’).value == “”) {
$w(‘#reqLastname’).show();
setTimeout(function() {
$w(‘#reqLastname’).hide();
}, 3000)
}
$w(‘#image5’).show();
let toInsertCards = {
“primaryId” : primaryId,
“firstname” : $w(‘#firstName’).value,
“middlename” : $w(‘#middleName’).value,
“lastname” : $w(‘#lastName’).value,
“jobtitle” : $w(‘#jobTitle’).value,
“department” : $w(‘#department’).value,
“company” : $w(‘#company’).value,
“cardname” : $w(‘#cardName’).value,
“image” : $w(‘#image1’).src,
“cardlanguage” : Number($w(‘#languageDropdown’).value),
“useremail” : Email
}

if ($w(‘#firstName’).valid && $w(‘#middleName’).valid && $w(‘#lastName’).valid && $w(‘#jobTitle’).valid && $w(‘#department’).valid
&& $w(‘#company’).valid && $w(‘#cardName’).valid)
{
await wixData.insert(“cards”, toInsertCards) // Insertion

  id = await wixData.query('cards') 
  .eq('primaryId', primaryId) 
  .find() 
  .then((result) => { 
    if (result.items.length > 0) { 
        return result.items[0]._id; 
    } else { 
        console.warn('No results were found') 
    } 
}) 
 await addcardsdetails(id);  
 await wixLocation.to("/cards");            
  
  } 

}
/** add end **/

//** edit **/
else if(taskName == “edit”) {
$w(‘#image5’).show();
let toSaveCards = {
“_id” : id,
“primaryId” : primaryId,
“firstname” : $w(‘#firstName’).value,
“middlename” : $w(‘#middleName’).value,
“lastname” : $w(‘#lastName’).value,
“jobtitle” : $w(‘#jobTitle’).value,
“department” : $w(‘#department’).value,
“company” : $w(‘#company’).value,
“cardname” : $w(‘#cardName’).value,
“image” : $w(‘#image1’).src,
“cardlanguage” : Number($w(‘#languageDropdown’).value),
“useremail” : Email
}
await wixData.save(“cards”, toSaveCards) // Insertion
await removecardsdetails(id);
await addcardsdetails(id);
await wixLocation.to(“/cardsdetails/”+ primaryId);
}
}
}

This is the remove function ( because each time the user use edit I delete the records from child table and add them with the new entries

function removecardsdetails(id) {
wixData.query(“cardsdetailes”)
.eq(“foreignkey”, id)
.find()
.then((res ) => {
res.items.forEach(item => {
wixData.remove(“cardsdetailes”, item._id);
})
})
}


This is the add frinction for the child table

function addcardsdetails(id) {
let toInsertArray = [];
//------ insert phone
if ($w(‘#phoneInput’).value !==“”) {
let ToInsertPhone = {
“socialname” : $w(‘#phoneText’).text,
“socialid” : $w(‘#phoneInput’).value,
“url” : “tel:” + $w(‘#phoneInput’).value,
“socialimage” : $w(‘#phoneImage’).src,
“sortkey” :1,
“foreignkey” : id

        }       
       toInsertArray.push(ToInsertPhone) 
       }             
        //------ insert email 
        if ($w('#emailInput').value !== "") { 
         let ToInsertEmail = { 
         "socialname"  : $w('#emailText').text, 
         "socialid"    : $w('#emailInput').value, 
         "url"         : "mailto:" + $w('#emailInput').value, 
         "socialimage"  : $w('#emailImage').src, 
         "sortkey"      :2, 
         "foreignkey"   : id 
        } 
       toInsertArray.push(ToInsertEmail)}             
        //------ insert Link 
        if ($w('#linkInput').value !=="") { 
         let ToInsertLink = { 
         "socialname"  : $w('#linkText').text, 
         "socialid"    : $w('#linkInput').value, 
         "url"         : "http://" + $w('#linkInput').value, 
         "socialimage"  : $w('#linkImage').src, 
         "sortkey"      :3, 
         "foreignkey"   : id 
        }       
       toInsertArray.push(ToInsertLink) 
       } 
        //------ insert website 
        if ($w('#websiteInput').value !=="") { 
         let ToInsertWebsite = { 
         "socialname"  : $w('#websiteText').text, 
         "socialid"    : $w('#websiteInput').value, 
         "url"         : "http://" + $w('#websiteInput').value, 
         "socialimage"  : $w('#websiteImage').src, 
         "sortkey"      :4, 
         "foreignkey"   : id 
        }       
       toInsertArray.push(ToInsertWebsite) 
       }       
        //------ insert twitter 
        if ($w('#twitterInput').value !== "") { 
         let ToInsertTwitter = { 
         "socialname"  : $w('#twitterText').text, 
         "socialid"    : $w('#twitterInput').value, 
         "url"         :  $w('#twitterInput').value, 
         "socialimage" : $w('#twitterImage').src, 
         "sortkey"     :5, 
         "foreignkey"  : id 
        }                
        toInsertArray.push(ToInsertTwitter)  
        }             
        //------ insert snapchat 
        if ($w('#snapchatInput').value !=="") { 
         let ToInsertSnapchat = { 
         "socialname"  : $w('#snapchatText').text, 
         "socialid"    : $w('#snapchatInput').value, 
         "url"         :  $w('#snapchatInput').value, 
         "socialimage"  : $w('#snapchatImage').src, 
         "sortkey"      :6, 
         "foreignkey"   : id 
        }       
        toInsertArray.push(ToInsertSnapchat) 
        }             
        //------ insert instagram 
        if ($w('#instagramInput').value !=="") { 
         let ToInsertInstagram = { 
         "socialname"  : $w('#instagramText').text, 
         "socialid"    : $w('#instagramInput').value, 
         "url"         :$w('#instagramInput').value, 
         "socialimage"  : $w('#instagramImage').src, 
         "sortkey"      :7, 
         "foreignkey"   : id 
        }       
        toInsertArray.push(ToInsertInstagram)           
          } 
        //------ insert tiktok 
        if ($w('#tiktokInput').value !=="") { 
         let ToInsertTiktok = { 
         "socialname"  : $w('#tiktokText').text, 
         "socialid"    : $w('#tiktokInput').value, 
         "url"         : $w('#tiktokInput').value, 
         "socialimage"  : $w('#tiktokImage').src, 
         "sortkey"      :8, 
         "foreignkey"   : id 
        }       
        toInsertArray.push(ToInsertTiktok) 
                    } 
        //------ insert linkdin 
        if ($w('#linkdinInput').value !=="") { 
         let ToInsertLinkdin = { 
         "socialname"  : $w('#linkdinText').text, 
         "socialid"    : $w('#linkdinInput').value, 
         "url"         : $w('#linkdinInput').value, 
         "socialimage"  : $w('#linkdinImage').src, 
         "sortkey"      :9, 
         "foreignkey"   : id 
        }                 
        toInsertArray.push(ToInsertLinkdin) 
       } 
        //------ insert whatsapp 
        if ($w('#whatsappInput').value !=="") { 
         let ToInsertWhatsapp = { 
         "socialname"  : $w('#whatsappText').text, 
         "socialid"    : $w('#whatsappInput').value, 
         "url"         : $w('#whatsappInput').value, 
         "socialimage"  : $w('#whatsappImage').src, 
         "sortkey"      :10, 
         "foreignkey"   : id 
        }       
        toInsertArray.push(ToInsertWhatsapp) 
        } 
        //------ insert facebook 
        if ($w('#facebookInput').value !=="") { 
         let ToInsertFacebook = { 
         "socialname"  : $w('#facebookText').text, 
         "socialid"    : $w('#facebookInput').value, 
         "url"         :$w('#facebookInput').value, 
         "socialimage"  : $w('#facebookImage').src, 
         "sortkey"      :11, 
         "foreignkey"   : id 
        }       
        toInsertArray.push(ToInsertFacebook) 
        } 
        //------ insert youtube 
        if ($w('#youtubeInput').value !=="") { 
         let ToInsertYoutube = { 
         "socialname"  : $w('#youtubeText').text, 
         "socialid"    : $w('#youtubeInput').value, 
         "url"         : $w('#youtubeInput').value, 
         "socialimage"  : $w('#youtubeImage').src, 
         "sortkey"      :12, 
         "foreignkey"   : id 
        }       
        toInsertArray.push(ToInsertYoutube) 
       } 
  wixData.bulkInsert("cardsdetailes",toInsertArray) 

}


Any help please

Hi! Check the data that you are trying to save to make sure that it is valid and meets the requirements of the fields in the database. For example, if a field is required and you are trying to save an empty string to it, this could cause an error. Consider using transactions to ensure that the data is saved to both databases atomically. This will allow you to roll back any changes if an error occurs while saving the data to either database. Double-check your query conditions to make sure that you are querying the correct data and that you are using the correct field names and values. Make sure that you have proper permissions to access and modify the data in both databases. Write if the Console.log say somthing and post the screenshot for more details. It’s possible that there is an issue with the uuid() function, which generates a new ID for the card. If this function is not returning a valid ID, the data may not be saved correctly. Also possible that there is an issue with the wixData.query() function, which is used to retrieve data from the database. If this function is not returning the expected results, it could cause the data to be deleted.