我正在创建第一个phonegap应用程序,以便我遵循一个教程int,以便在我的logcat中抛出以下错误。
未捕获的TypeError:无法调用未定义的方法'transaction' 文件:///android_asset/www/js/index.js:40
“*”行是我的第40行
function saveDatas(details, cb) {
if(details.name == "") details.name = "[No Title]";
*dbShell.transaction(function(tx) {*
if(details.id == "")tx.executeSql("insert into nameDetail(name,age,city,occupation,date) values(?,?,?,?,?)",[details.name,details.age,details.city,details.occupation, new Date()]);
else tx.executeSql("update nameDetail set name=?, age=?,city=?,occupation=?,date=? where id=?",[details.name,details.age,details.city,details.occupation, new Date(), details.id]);
}, dbErrHandler,cb);
}
编辑1:
var dbShell;
function phoneReady() {
dbShell = window.openDatabase("Names", 1, "Names", 1000000);
dbShell.transaction(setUpTable, dbErrHandler, getDatas);
}
完整的js文件:
var dbShell;
function phoneReady() {
dbShell = window.openDatabase("Names", 1, "Names", 1000000);
dbShell.transaction(setUpTable, dbErrHandler, getDatas);
}
function dbErrHandler(err) {
alert("Error : " + err.message + "\n Code" + err.code); }
function setUpTable(tx) {
tx.executeSql("CREATE TABLE IF NOT EXISTS nameDetail(id INTEGER PRIMARY KEY,name,age,city,occupation,date)");
}
function getDatas() {
dbShell.transaction(function(tx) {
tx.executeSql("select name,age,city,occupation,date from nameDetail order by date desc", [], renderEntries, dbErrHandler);
}, dbErrHandler); }
function renderEntries(tx, results) {
if (results.rows.length == 0) {
$("#mainContent").html("<p>Don't have any Details</p>");
} else {
var s = "";
for (var i = 0; i < results.rows.length; i++) {
s += "<li><a href='addEdit.html?id=" + results.rows.item(i).id + "'>" + results.rows.item(i).title + "</a></li>";
}
$("#noteTitleList").html(s);
$("#noteTitleList").listview("refresh");
} }
function saveDatas(details, cb) { if(details.name == "") details.name = "[No Title]";
dbShell.transaction(function(tx) {
if(details.id == "")tx.executeSql("insert into nameDetail(name,age,city,occupation,date) values(?,?,?,?,?)",[details.name,details.age,details.city,details.occupation, new Date()]);
else tx.executeSql("update nameDetail set name=?, age=?,city=?,occupation=?,date=? where id=?",[details.name,details.age,details.city,details.occupation, new Date(), details.id]);
}, dbErrHandler,cb);
}
function init() {
document.addEventListener("deviceReady", phoneReady, false);
$("#addEditForm").live("submit", function(e) {
var data = {
name:$("#mName").val(),
age:$("#mAge").val(),
city:$("#mCity").val(),
occupation:$("#mOccupation").val(),
id:$("#mId").val()
};
saveDatas(data, function() {
$.mobile.changePage("index.html", {
reverse: true
});
});
e.preventDefault();
});
$("#homePage").live("homepage", function() {
getDatas();
});
$("#editPage").live("pageshow", function() {
var loc = window.location.hash;
if (loc.indexOf("?") >= 0) {
var qs = loc.substr(loc.indexOf("?") + 1, loc.length);
var detailId = qs.split("=")[1];
$("editFormSubmitButton").attr("disabled", "disabled");
dbShell.transaction(function(tx) {
tx.executeSql("select id,name,age,city,occupation from nameDetails where id=?", [detailId], function(tx, results) {
$("#mId").val(results.rows.item(0).id);
$("#mName").val(results.rows.item(0).name);
$("#mAge").val(results.rows.item(0).age);
$("#mCity").val(results.rows.item(0).city);
$("#mOccupation").val(results.rows.item(0).occupation);
$("#editFormSubmitButton").removeAttr("disabled");
});
}, dbErrHandler);
} else {
$("#editFormSubmitButton").removeAttr("disabled");
}
});
}
任何人都建议一些解决方案......
答案 0 :(得分:0)
在JavaScript中,声明和定义存在差异。
var dbShell;
现在dbShell已声明。
dbShell = "foo";
现在dbShell 已定义。
如果你这样做:
var dbShell;
dbShell.foo();
您将获得:Uncaught TypeError: Cannot call method 'foo' of undefined