我是JavaScript新手,嘗試編寫Firefox附加組件。
我想通過/提取從SQL查詢返回到調用函數的數據。 它似乎沒有工作。
我搜索了關於變量範圍的信息,查看了我在本網站上看到的任何相關文章,並嘗試了據說可行的示例,但沒有一個適用於我。從executeAsync將數據返回到調用函數(javascript/mozilla)
我使用的存儲信息來源:
https://developer.mozilla.org/en/Storage
https://developer.mozilla.org/en/mozIStorageStatement
,該怎麼辦錯了,還是我應該怎麼做呢?
我在xp上運行firefox 12。
function Sqlite() {
this.dbConn = "";
this.empty = true;
}
Sqlite.prototype.retrieveData = function(query)
{
var rows = new Array(); // to be returned to the calling function
var stmt = this.dbConn.createStatement(query); // my "select..." statement
stmt.executeAsync ({
handleResult: function(aResultSet)
{
this.empty = false;
var i = 0;
for (let row; row = aResultSet.getNextRow();i++) {
rows[i] = row;
}
},
handleError: function(anError) {
//some code
},
handleCompletion: function(aReason) {
if (this.empty) {
// CODE FOR WHEN STATEMENT EXECUTION IS FINISHED
}
if (aReason == Ci.mozIStorageStatementCallback.REASON_FINISHED) {
document.getElementById("debug").value = rows[0]; //gets the data from the query: [xpconnect wrapped mozIStorageRow]
return true;
}
}
});
document.getElementById("debug").value = rows[0]; //gets undefined
stmt.finalize();
return rows; // the info is no longer available at this point.
}
我會很感激你的幫助
對不起,我覺得我很清楚。但是你明白了,因爲你讓我找到了解決方案。 謝謝。 (我提取的列,但它是不相關的問題) 這是我做的:
但是,這是一個優雅的方式來做到這一點? – Laurel我會給你投票,但我太新了。沒有足夠的代表。 再次感謝您 – Laurel
我認爲您將請求成功返回零行(因爲表爲空)與請求失敗(因爲數據庫已損壞,例如)混淆。否則,是的,這聽起來很好。 –