下面的按钮正在调用函数Sync(),函数sync将调用多个函数,其中一个是syncLocation(),我要按顺序调用这些函数(当syncLocation返回true时,执行下一个)。 即使使用异步并等待功能syncLocation也返回未定义!
我无法找到问题所在!
<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;
});
}
我尝试改用in函数变量,但仍然没用:
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;
}