Utilizar objeto entre as funções Javascript - Phonegap

Asked

Viewed 298 times

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&deg; '+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.

  • 2

    It seems that you are calling the function inserDailyservices before you called initialize and onDeviceReady.

  • Worse q no, it is called after... In html file there is a app.initialize() and then a app.inserDailyServices()

  • 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.

  • I updated at the end how the method is being called in the html file.

  • 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 the onDeviceReady (that is, from within, or within some other function called in the onDeviceReady). This supposing that the onDeviceReady is properly "armed" by bindEvents.

  • Yes, the onDeviceReady is inside the bindEvents and the method insertDailyServices is being called within the onDeviceReady, to be more exact is the last line and code inside the onDeviceReady, which in this case is the call to my method.

Show 1 more comment

1 answer

1

The initialize causes app.db is null. You could do this on initialize:

this.db = window.sqlitePlugin.openDatabase({name:'4routes'});

It seems that with this SQLitePlugin you need to call the openDatabase within the onDeviceReady, if you call it that:

<script type="text/javascript">
    app.initialize();
    app.listOpenServices();
</script>

This may be a problem. Remove it from HTML and leave it as follows in javascript:

var app = {
    initialize: function() {
        this.bindEvents();
        this.db = window.sqlitePlugin.openDatabase({name:'4routes'});;
    },
    ...
    onDeviceReady: function() {
        this.initialize();
        this.listOpenServices();
        ...
  • I’ll test and I’ll give you an answer.

  • Now he changed the answer: `09-02 12:48:50.364: I/Web Console(24544): Sqliteplugin openargs: {"name":"4routes"} at file:///android_asset/www/plugins/com.brodysoft.sqlitePlugin/www/Sqliteplugin.js:39``

  • I think you are calling the openDatabase of this plugin too early, see the updated response.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.