0

The below button is calling the function Sync(), the function sync will call multiple functions one of them is the syncLocation(), I want to call the functions in order (when syncLocation return true execute the next one). Even using async and await the function syncLocation is returning undefined!

I am unable to find what's going wrong !

<button style="color:#2CA2D5;" onclick="Sync();">Sync</button> 

 async function Sync(){

        logString = "Initiating sync... <br> Start preparing location JSON string... <br> ";
        document.getElementById("log").innerHTML = logString;

        alert(await syncLocation()); // return undefined !!!!
        if (await syncLocation() == true){

            logString = logString + "Done preparing location JSON string. <br> Start sending location JSON string to WCF... <br>";
            document.getElementById("log").innerHTML = logString;

            if (await sendJsonToWCF(JSONcustlocation) == true){

                if (await ClearTableCustlocation() == true){

                    logString = logString + "Start preparing image JSON string... <br>";
                    document.getElementById("log").innerHTML = logString;

                    if (await syncImages() == true){

                        logString = logString + "Done preparing image JSON string. <br> Start sending image JSON string to WCF... <br>";
                        document.getElementById("log").innerHTML = logString;

                        if (await sendJsonToWCF(JSONcustimage) == true){

                            if (await ClearTableCustimage()== true){

                                logString = logString + "Updating table Customers... <br>";
                                document.getElementById("log").innerHTML = logString;

                                if (await getCustomers() == true){

                                    logString = logString + "Successfully Synced! <br>";
                                    document.getElementById("log").innerHTML = logString;
                                    //window.location.href = 'index.html';

                                }else{alert("ERROR getCustomers: KINDLY CONTACT SUPPORT");}
                            }else{alert("ERROR ClearTableCustimage: KINDLY CONTACT SUPPORT");}
                        }else{alert("ERROR sendJsonToWCF: KINDLY CONTACT SUPPORT");}
                    }else{alert("ERROR syncImages: KINDLY CONTACT SUPPORT");}
                }else{alert("ERROR ClearTableCustlocation: KINDLY CONTACT SUPPORT");}
            }else{alert("ERROR sendJsonToWCF: KINDLY CONTACT SUPPORT");}
        }else{alert("ERROR syncLocation: KINDLY CONTACT SUPPORT");}

    }

function syncLocation(){

        //Preparing JSON for table CustLocation
        var locationcount = 0;
        db.executeSql('SELECT count(*) AS locationcount FROM CustLocation', [], function(rsl) {
            locationcount = rsl.rows.item(0).locationcount;

            db.executeSql('SELECT * FROM CustLocation', [], function(rs) {

                JSONcustlocation = JSONcustlocation.concat('[');
                for (i = 0; i <= locationcount -1; i++) {
                    if (i > 0){ JSONcustlocation = JSONcustlocation.concat(','); }

                    //CustomerBarcode, UserCode, Long, Lat, StampDate
                    JSONcustlocation = JSONcustlocation.concat('{"CustomerBarcode" : "' + rs.rows.item(i).CustomerBarcode + '", "UserCode" : "' + localStorage.getItem("userid") + '", "Long" : "' + rs.rows.item(i).Long + '", "Lat" : "' + rs.rows.item(i).Lat + '", "StampDate" : "' + rs.rows.item(i).StampDate + '"}');
                }
                JSONcustlocation = JSONcustlocation.concat(']');

                return true;

            }, function(error) {
                alert('SELECT SQL statement ERROR while building JSONcustlocation: ' + error.message);
                return false;
            });

        }, function(error) {
            alert('SELECT SQL statement ERROR: ' + error.message);
            return false;
        }); 

    }

I tried to use a in function variable instead but still didn't worked :

    function syncLocation(){
    var bool = false;

        //Preparing JSON for table CustLocation
        var locationcount = 0;
        db.executeSql('SELECT count(*) AS locationcount FROM CustLocation', [], function(rsl) {
            locationcount = rsl.rows.item(0).locationcount;

            db.executeSql('SELECT * FROM CustLocation', [], function(rs) {

                JSONcustlocation = JSONcustlocation.concat('[');
                for (i = 0; i <= locationcount -1; i++) {
                    if (i > 0){ JSONcustlocation = JSONcustlocation.concat(','); }

                    //CustomerBarcode, UserCode, Long, Lat, StampDate
                    JSONcustlocation = JSONcustlocation.concat('{"CustomerBarcode" : "' + rs.rows.item(i).CustomerBarcode + '", "UserCode" : "' + localStorage.getItem("userid") + '", "Long" : "' + rs.rows.item(i).Long + '", "Lat" : "' + rs.rows.item(i).Lat + '", "StampDate" : "' + rs.rows.item(i).StampDate + '"}');
                }
                JSONcustlocation = JSONcustlocation.concat(']');

                bool = true;

            }, function(error) {
                alert('SELECT SQL statement ERROR while building JSONcustlocation: ' + error.message);
                bool = false;
            });

        }, function(error) {
            alert('SELECT SQL statement ERROR: ' + error.message);
            bool =false;
        }); 
     return bool;
    }
Tony
  • 1,099
  • 9
  • 19

0 Answers0