Single success message after multiple collections insert

I am updating multiple collections from form submission. My question is how to code having just a single success or failure message after all the insert events have completed? Appreciate any advice that would point me in the right direction.
My code:

export function formSubmit_click(event, $w) {
 const toInsert1 = {
 'company': $w("#companyName").value,
 'contactFN': $w("#firstName").value,
 'contactLN': $w("#lastName").value,
 'contactEmail': $w("#email").value,
 'contactTel': $w("#telephone").value,
 'contactPos': $w("#position").value,
 'contactAddr': $w("#address").value,
 'contactPrevprac': $w("#prevAcc").value,
 'contactPrevacc': $w("#prevaccName").value,
 'contactPrevtel': $w("#prevaccTel").value,
 'contactPrevemail': $w("#prevaccEmail").value,
 'contactPrevaddr': $w("#prevaccAddress").value,
 'contactNoprev': $w("#noPrevacc").checked
 };
 
 const toInsert2 = {
 'company': $w("#companyName").value,
 'compUTR': $w("#companyUTR").value,
 'compVAT': $w("#vatRegistration").value,
 'compVATdate': $w("#effecVATdate").value,
 'compVATscheme': $w("#schemeVAT").value,
 'compEmpl': $w("#emplReference").value,
 'compCIS': $w("#contractorNumb").value,
 'compAuth': $w("#chAuth").value,
 'compSoftw': $w("#softwareYN").value,
 'compSname': $w("#softwName").value,
 'xHelp': $w("#xeroHelp").value,
 'xHelpdetail': $w("#helpDetail").value
 };

    wixData.insert("clients", toInsert1)
 .then( (results) => {
 let items = results;
 })
 .catch( (err) => {
 let errorMsg = err;
 });

    wixData.insert("system", toInsert2)
 .then( (results) => {
 let items = results;
 })
 .catch( (err) => {
 let errorMsg = err;
 });
}

In your case i just remember → promiseAll() or something like that.

Another option would be to handle/run your inserts one by another and not simultaniuosly.

wixData.insert("clients", toInsert1)
.then((results)=>{
let items = results;
wixData.insert("system", toInsert2)
.then((results)=>{
let items = results;
// Success msg here.
})
.catch((err)=>{
// Error message here for second database
let errorMsg = err;
});
}).catch((err)=>{
// Error message here for first database
let errorMsg = err;
});

Thank you, works just as I wanted.

Hi @dean41146 :raised_hand_with_fingers_splayed:

@team40951 's example works well, but it’s not the right approach for this scenario, you don’t need to wait for the first insert to start the second insert, this will only increase the processing time (delay) significantly, especially if you have many promises, the best approach is to use the JavaScript Promise.all( ) method, this will run all your promises at the same time (reducing delay) and then display the success message if all your promises are fulfilled, keep in mind that if any promise has failed, the chained promise will also fail.

Here’s an example:

function insertAll() {
    return Promise.all([
        wixData.insert("clients", toInsert1),
        wixData.insert("system", toInsert2)
    ]).then((results) => {
        const clients_result = results[0];
        const system_result = results[1];
        // Show the success message
        $w('#msg').text = 'Success!';
        return Promise.resolve();
    }).catch(err => {
        return Promise.reject(err);
    })
}

Hope this helps~!
Ahmad

Thank you Ahmad. This is very helpful

@dean41146 glad tht you find it so :blush: