我的代码:
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控制台中的内容:
查询已完成:{}
查询已完成:{}
未捕获的TypeError:无法读取属性' message'未定义的
完成了两个查询,所以我猜它已成功创建并插入。我试图显示它们时会显示数据。
但是,它是不是应该在{}
内显示一些数据?
而且我不知道为什么会这样:
未捕获的TypeError:无法读取属性' message'未定义的
即使插入了数据。上面的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>
答案 0 :(得分: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);
}