1
I’m creating in onDeviceReady
an object to create the connection to the application’s Sqlite database. It is being created as follows:
var app = {
initialize: function() {
this.bindEvents();
this.db = null;
},
...
onDeviceReady: function() {
app.db = window.sqlitePlugin.openDatabase({name:'4routes'});
...
However, there is a method called insertDailyServices
which is responsible for storing the information in the bank and the same is returning error.
insertDailyServices: function(service) {
app.db.transaction(function(tx){
tx.executeSql('INSERT INTO file_servico (id_servico,id_os,id_fornecedor,id_motorista,id_veiculo,origem,destino,inicio,termino,created_at) VALUES (?,?,?,?,?,?,?,?,?,?)',
[service.id_servico,service.id_file,service.id_fornecedor,service.id_motorista,service.id_veiculo,service.origem,service.destino,service.inicio,service.termino,moment().format('YYYY-MM-DD HH:mm:ss')],
function(tx,res){
$('#listServicos').append('Serviço de N° '+service.id_servico+' registrado localmente.<br>');
});
});
}
Error presented
08-31 01:54:03.100: E/Web Console(31269): Uncaught TypeError: Cannot call method 'transaction' of undefined at file:///android_asset/www/js/index.js:136
File call:
<script type="text/javascript">
app.initialize();
app.listOpenServices();
</script>
How to solve this problem??? Just need to pass the app.db
to be read from any method inside the file.
It seems that you are calling the function
inserDailyservices
before you calledinitialize
andonDeviceReady
.– hugomg
Worse q no, it is called after... In html file there is a
app.initialize()
and then aapp.inserDailyServices()
– Ewerton Melo
Are you sure it’s after? A very common error in Javascript is mixing asynchronous code (callbacks) with synchronous code. The result is that some things run ahead of expected.
– hugomg
I updated at the end how the method is being called in the html file.
– Ewerton Melo
And where is the call from
insertDailyServices
? What @hugomg said is correct. The "after" he refers to is in time, not in the position in the source code. It needs to be after theonDeviceReady
(that is, from within, or within some other function called in theonDeviceReady
). This supposing that theonDeviceReady
is properly "armed" bybindEvents
.– bfavaretto
Yes, the
onDeviceReady
is inside the bindEvents and the methodinsertDailyServices
is being called within theonDeviceReady
, to be more exact is the last line and code inside theonDeviceReady
, which in this case is the call to my method.– Ewerton Melo