我使用html5和javascript创建移动Web应用程序。我有两个javascript文件。 AttributesDatabase.js和AttributeView.js.From AttributeView.js我从AttributeDatabase.js中调用一个函数,即我执行一个select查询。现在查询结果应该转到AtttributeView.js.But Websql事务是异步调用那是什么呢没有返回正确的结果。有没有办法处理websql结果。 请帮忙,如果有的话?
被修改
AttributeView.js
var AttributeDAOObj = new AttributeDAO();
AttributeDAOObj.GetAttributeList();
alert(AttributeDAOObj.GetAttributeList()); //This alert is coming as undefined.
AttributeDAO.js
this.GetAttributeList = function () {
var baseDAOObj = new BaseDAO();
var query = "SELECT AttributeName FROM LOGS";
// this.Successcalbackfromsrc = this.myInstance.Successcalback;
var parm = { 'query': query, 'Successcalback': this.myInstance.Successcalback };
baseDAOObj.executeSql(parm);
}
//To Create database and execute sql queries.
function BaseDAO() {
this.myInstance = this;
//Creating database
this.GetMobileWebDB = function () {
if (dbName == null) {
var dbName = 'ABC';
}
var objMobileWebDB = window.openDatabase(dbName, "1.0", dbName, 5 * 1024 * 1024);
return objMobileWebDB;
}
//Executing queries and getting result
this.executeSql = function (query) {
var objMobileWebDB = this.myInstance.GetMobileWebDB();
objMobileWebDB.transaction(function (transaction) {
//In this transaction i m returning the result.The result value is coming.
transaction.executeSql(query, [], function (transaction, result) { return result; }, this.Errorclback);
});
}
}
答案 0 :(得分:0)
问题在于你成功回电(比如你的问题的评论,由DCoder说明)
function (transaction, result) { return result; }
这是回到哪里?
所以这是如何做到的(或者至少是一种方式)
你可以这样做:
function (transaction,result){
console.log("yes, I have some result, but this doesn't say anything, empty result gives also a result");
// so check if there is a result:
if (result != null && result.rows != null) {
if (result.rows.length == 0) {
// do something if there is no result
}else{
for ( var i = 0; i < result.rows.length; i++) {
var row = result.rows.item(i);
var id = result.rows.item(i).id; //supposing there is an id in your result
console.log('Yeah! row id = '+id);
}
}
}else{
// do something if there is no result
}
};
请注意,上面的代码可以更加紧凑,但这是如何更好地理解它。
另一种方法是把这个函数放在一个单独的代码片段中,这样你就可以保持sql语句更加紧凑和可读。就像你称之为错误回调一样,这可以在你的函数中(在它之前。)或一个完全独立的函数。