WebSQL 错误回调中的“无法读取未定义的属性‘消息’”

Posted

技术标签:

【中文标题】WebSQL 错误回调中的“无法读取未定义的属性‘消息’”【英文标题】:"Cannot read property 'message' of undefined" in WebSQL error callback 【发布时间】:2015-12-09 13:47:02 【问题描述】:

我的代码:

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 控制台中的内容:

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

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

但是它不应该在 中显示一些数据吗?

我不知道为什么会得到:

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

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

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

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>

【问题讨论】:

【参考方案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);

【讨论】:

哦..所以未捕获的类型错误毕竟来自 db.transaction :p 我切换了它们,现在没有错误。谢谢你^u^

以上是关于WebSQL 错误回调中的“无法读取未定义的属性‘消息’”的主要内容,如果未能解决你的问题,请参考以下文章

ng 构建消息“无法读取未定义的属性 'toLowerCase' 中的错误”

挂载钩子中的错误:“TypeError:无法读取未定义的属性 'allStage'”

测试组件 - 挂载钩子中的错误:“TypeError:无法读取未定义的属性 'dispatch'”

指令 Dragula 更新挂钩中的错误:“TypeError:无法读取未定义的属性 'drake'”

Javascript 中的数组错误:未捕获的 TypeError:无法读取未定义的属性“x”

NextJS:未捕获的类型错误:无法读取未定义的属性(读取“属性”)