2

我的代码:

function onSuccess(transaction, resultSet) {
    console.log('Query completed: ' + JSON.stringify(resultSet));
}
function onError(transaction, err) {
    console.log("Error : " + err.message);
}
function createTBinventorymastercatalogue(tx) {
    var query = "";
    query += "CREATE TABLE IF NOT EXISTS INVENTORY_MASTER_CATALOGUE(SysPk_InvtyCat,SysFk_Invty_InvtyCat);";
    tx.executeSql(query, [], onSuccess, onError);
    tx.executeSql("INSERT INTO INVENTORY_MASTER_CATALOGUE(SysPk_InvtyCat, SysFk_Invty_InvtyCat) VALUES (?,?)", ['dummydata-pk', 'dummydata-fk'], onSuccess, onError);
}

这是我的 JavaScript 控制台中的内容:

查询完成:{}
查询完成:{}
Uncaught TypeError: Cannot read property 'message' of undefined

两个查询完成,所以我猜它成功创建并插入。当我尝试显示它们时,数据会显示。

但是它不应该在里面显示一些数据{}吗?

而且我不知道为什么会得到:

未捕获的类型错误:无法读取未定义的属性“消息”

即使数据被插入。上面的 2 个查询是应用启动时运行的唯一查询。

我有其他查询,但它们仅在单击按钮时运行,因此错误不可能是由这些引起的。我不知道第三个日志是从哪里来的,为什么“消息”是未定义的?

MCVE

JavaScript:

var ref = null;


(function () {
    "use strict";
    document.addEventListener( 'deviceready', onDeviceReady.bind( this ), false );
})();

var dbSize = 5 * 1024 * 1024; // 5MB

var db = openDatabase("Todo", "", "Todo manager", dbSize, function () {    console.log('db successfully opened or created');});


db.transaction(createTBinventorymastercatalogue, onSuccess, onError);


function onSuccess(transaction, resultSet) {

    console.log('Query completed: ' + JSON.stringify(resultSet));
}

function onError(transaction, err) {
    console.log("Error : " + err.message);

}



function createTBinventorymastercatalogue(tx)
{

    var query = "";

    query += "CREATE TABLE IF NOT EXISTS INVENTORY_MASTER_CATALOGUE(SysPk_InvtyCat,SysFk_Invty_InvtyCat);";
    tx.executeSql(query, [], onSuccess, onError);

    tx.executeSql("INSERT INTO INVENTORY_MASTER_CATALOGUE(SysPk_InvtyCat, SysFk_Invty_InvtyCat) VALUES (?,?)", ['dummydata-pk', 'dummydata-fk'], onSuccess, onError);
}


function renderinventorymastercatalogue()
{
    alert('rendering');
    db.transaction(function (tx2) {
        tx2.executeSql('SELECT * FROM INVENTORY_MASTER_CATALOGUE', [], showInventoryMasterCatalogueData, onSuccess, onError);
    });
}


function showInventoryMasterCatalogueData(tx,results) {

    var invtritemsstring = '';

   for (var ind = 0; ind < results.rows.length ; ind++)
    {
        invtritemsstring += results.rows.item(ind).SysPk_InvtyCat + '-' + results.rows.item(ind).SysFk_Invty_InvtyCat + '<br/>';
    }
    $('.Data-cont').html(invtritemsstring);

}


$('body').on('click', '#show-invtycatmstr-btn', function () { renderinventorymastercatalogue(); });


function deleteAllFromInventoryMasterCatalogue()
{

    alert('delete from table');
    db.transaction(function (tx3) {
        tx3.executeSql('Delete from INVENTORY_MASTER_CATALOGUE', [], renderinventorymastercatalogue, onError);
    });
}

$('body').on('click', '#deleteallfrominvtycatmstr-btn', function () { deleteAllFromInventoryMasterCatalogue(); });


function insertdata(tx)
{
    var enteredsyspk = $('#syspk-inputtext').val();
    var enteredsysfk = $('#sysfk-inputtext').val();





    db.transaction(function (tx4, enteredsyspk, enteredsysfk)
    {
        alert(enteredsyspk + ' ' + enteredsysfk);
        tx4.executeSql('INSERT INTO INVENTORY_MASTER_CATALOGUE(SysPk_InvtyCat, SysFk_Invty_InvtyCat) VALUES (?,?)', ['enteredsyspk', 'enteredsysfk'], renderinventorymastercatalogue, onError);

    });


}


$('body').on('submit', '#syspksysfk-form', function (event)
{
    event.preventDefault();
    insertdata();
});

html:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />


    <meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *">
    <title>BlankCordovaApp3</title>


</head>
<body>
    <div id="content-container">

        <form id="syspksysfk-form">
            <input type="text" name="syspk" id="syspk-inputtext" value="" required/>
            <input type="text" name="sysfk" id="sysfk-inputtext" value="" required />
            <br />
            <input type="submit" value="submit" id="submitform-btn" />
        </form>
                <br />
        <button id="show-invtycatmstr-btn">Show InvtyCatMstr</button>
        <button id="deleteallfrominvtycatmstr-btn">Delete All From InvtyCatMstr</button>


        <div class="Data-cont"></div>
    </div>

<script src="scripts/jquery-2.1.4.min.js"></script>
<!-- Cordova reference, this is added to your app when it's built. -->
<script src="cordova.js"></script>
<script src="scripts/platformOverrides.js"></script>

<script src="scripts/index.js"></script>

4

1 回答 1

1

调用时您的成功/错误回调被反转db.transaction()

db.transaction(createTBinventorymastercatalogue, onSuccess, onError);

出于某种原因,方法首先接受错误回调。

交换它们,一切都很好:

var dbSize = 5 * 1024 * 1024; // 5MB

var db = openDatabase("Todo", "", "Todo manager", dbSize, function() {
  console.log('db successfully opened or created');
});

db.transaction(createTBinventorymastercatalogue, onError, onSuccess);

function onSuccess(transaction, resultSet) {
  console.log('Query completed: ' + JSON.stringify(resultSet));
}

function onError(transaction, err) {
  console.log("Error : " + err.message);
}

function createTBinventorymastercatalogue(tx) {
  var query = "";

  query += "CREATE TABLE IF NOT EXISTS INVENTORY_MASTER_CATALOGUE(SysPk_InvtyCat,SysFk_Invty_InvtyCat);";
  tx.executeSql(query, [], onSuccess, onError);

  tx.executeSql("INSERT INTO INVENTORY_MASTER_CATALOGUE(SysPk_InvtyCat, SysFk_Invty_InvtyCat) VALUES (?,?)", ['dummydata-pk', 'dummydata-fk'], onSuccess, onError);
}

于 2015-12-09T14:47:42.643 回答