My code:
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);
}
This is what's in my JavaScript console:
Query completed: {}
Query completed: {}
Uncaught TypeError: Cannot read property 'message' of undefined
Two queries completed, so I guess it successfully created and inserted. The data show when I try to display them.
But then isn't it supposed to show some data inside the {}
?
And I don't know why I get:
Uncaught TypeError: Cannot read property 'message' of undefined
Even though the data got inserted. The 2 queries above are the only queries that run when the app starts.
I have other queries but they only run when a button is clicked so the error could not have been caused by those. I don't know where the third log is from, and why is 'message' is undefined?
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>
Your success/error callbacks are reversed when calling
db.transaction()
:For some reason, that method takes the error callback first.
Swap them around and all is well: