Ionic sqlite Pre-Filled database showing error "Cannot read property 'transaction' of null"


#1

I am trying to get values from Pre-Filled database Using ionic, but i keep getting this error:

TypeError: Cannot read property ‘transaction’ of null
I am running this app on device…

ionicApp = angular.module('starter', ['ionic', 'ngCordova']);     
ionicApp.run(function($ionicPlatform, $cordovaSQLite) {
    $ionicPlatform.ready(function() {
        if(window.cordova && window.cordova.plugins.Keyboard) {
            cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
        }
        if(window.StatusBar) {
            StatusBar.styleDefault();
        }

        window.plugins.sqlDB.remove("populated.db",false,false);
        window.plugins.sqlDB.copy("populated.db", function() {
            db = $cordovaSQLite.openDB("populated.db");
        }, function(error) {           
            console.error("Error Code -----------> " + JSON.stringify(error));
        });
    });
    });

Controller

ionicApp.controller('ExampleController', function($scope,MyService,$ionicPlatform) {
  $ionicPlatform.ready(function() { 
    $scope.categories = MyService.getCategories();
  })      
})

MyService

ionicApp.service('MyService', function($cordovaSQLite) {
    this.getCategories = function() {
      var output = [];      
      query = "SELECT * FROM categories";
      $cordovaSQLite.execute(db, query).then(function(res) {     
        for (var i = 0; i < res.rows.length; i++) {
          output.push(res.rows.item(i));
        }    
        },function (err) {
            console.error(err);
        });      
      return output;
    }

index.html

<ul class="list">
  <li class="item" ng-repeat="category in categories">{{category.name}}</li>
</ul>

#2

Have your declared the db variable on top of the app.js file? Also it looks like the way you open the database is limited, try something like this:

            db = window.openDatabase('populated.db', '1.0', 'populated', 5 * 1024 * 1024);

In the past I had the same error and it was because I didn’t specify the size of the database. I hope this helps in any way.


#3

That is not problem. I am using $cordovaSQLite which don’t need size parameters, i think.
The problem is that “sqldb.copy” takes some time to copy the database and the controller is firing before “sqldb.copy” end up copying the database.
I don’t know how to fixed it.


#4

Hi sumitsmx. I wonder if you were able to solve your problem and if so , can you give us a hint because I have a similar problem and we are using similar code, thanks in advance , best wishes