简体   繁体   English

JavaScript中未捕获的类型错误

[英]Uncaught type error in javascript

I am creating the first phonegap application so that i followed one tutorial int that tut the following error should thrown in my logcat. 我正在创建第一个phonegap应用程序,因此我遵循了一个教程int的建议,认为应该在我的logcat中引发以下错误。

Uncaught TypeError: Cannot call method 'transaction' of undefined at file:///android_asset/www/js/index.js:40 Uncaught TypeError:无法在file:///android_asset/www/js/index.js:40处调用未定义的方法“事务”

" * " line is my 40th line “ *”行是我的第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);

}

Edit 1: 编辑1:

var dbShell;

function phoneReady() {
dbShell = window.openDatabase("Names", 1, "Names", 1000000);
dbShell.transaction(setUpTable, dbErrHandler, getDatas);

}

Full js file: 完整的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");
        }
    });

}

anybody suggest some solution ... 任何人都建议一些解决方案...

In JavaScript there is a difference in declaring and defining . 在JavaScript中, 声明定义有所不同。

var dbShell;

Now dbShell is declared . 现在, 声明 dbShell。

dbShell = "foo";

Now dbShell is defined . 现在已定义 dbShell。

If you do: 如果您这样做:

var dbShell;
dbShell.foo();

You will get: Uncaught TypeError: Cannot call method 'foo' of undefined 您将得到: Uncaught TypeError: Cannot call method 'foo' of undefined

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM