jeudi 10 mars 2016

"TypeError: Cannot read property 'transaction' of null" while using SQLite in an IONIC app

I'm facing this error while embedding SQLite in an ionic app i have read all the relevant articles but none help n this error persist. im sharing my app.js, controller.js and html file.


app.js

var starter=angular.module ('starter', ['ionic', 'ngCordova','starter.controllers']);


var db=null;


starter.run(
function ($ionicPlatform, $cordovaSQLite)
{  

   $ionicPlatform.ready 
   (
      function()
      {  
         if (window.cordova && window.cordova.plugins.Keyboard)
         {
            cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
            cordova.plugins.Keyboard.disableScroll(true);
         }
         if (window.StatusBar) 
         {
            StatusBar.styleDefault();
         }
         if(window.sqlitePlugin)
         {
             window.plugins.sqlDB.copy
             (
                 "Tour_MobileApp.db"
                 ,function () 
                 {
                     db = window.sqlitePlugin.openDatabase({name: "Tour_MobileApp.db"});
                 }
                 , function() 
                 {
                     console.error("There was an error copying the database: " + error);
                     db = window.sqlitePlugin.openDatabase({name: "Tour_MobileApp.db"});
                 }
             );  
         }
      }
   );
});  starter.config(function($stateProvider, $urlRouterProvider) {
$stateProvider
    .state
    (
        'app', 
        {
            url: '/app',
            abstract: true,
            templateUrl: 'templates/menu.html',
            controller: 'AppCtrl'
        }
    )
    .state
    (
        'app.home', 
        {
            url: '/home',
            views: 
            {
                'menuContent': 
                {
                    templateUrl: 'templates/home.html',
                    controller: 'homeCtrl'
                }
            } 
        }
    )
    .state
    (
        'app.places', 
        {
            url: '/places',
            views: 
            {
                'menuContent': 
                {
                    templateUrl: 'templates/places.html',
                    controller: 'placesCtrl'
                }
            }
        }
    )
    .state
    (
        'app.login',
        {
            url: '/login',
            views: 
            {
                'menuContent': 
                {
                    templateUrl: 'templates/login.html',
                }
            }
        }
    )
    .state
    (
        'app.browse',
        {
            url: '/browse',
            views: 
            {
                'menuContent': 
                {
                    templateUrl: 'templates/browse.html',
                }
            }
        }
    )
    .state
    (
        'app.rfort',
        {
            url: '/rfort',
            views: 
            {
                'menuContent': 
                {
                    templateUrl: 'templates/rfort.html',
                }
            }
        }
    )
    .state
    (
        'app.ltemple',
        {
            url: '/ltemple',
            views: 
            {
                'menuContent': 
                {
                    templateUrl: 'templates/ltemple.html',
                }
            }
        }
    )
    .state
    (
        'app.igate',
        {
            url: '/igate',
            views: 
            {
                'menuContent': 
                {
                    templateUrl: 'templates/igate.html',
                }
            }
        }
    )
    .state
    (
        'app.jmasjid',
        {
            url: '/jmasjid',
            views: 
            {
                'menuContent': 
                {
                    templateUrl: 'templates/jmasjid.html',
                }
            }
        }
    )
    .state
    (
        'app.guide',
        {
            url: '/guide',
            views: 
            {
                'menuContent': 
                {
                    templateUrl: 'templates/guide.html',
                }
            }
        }
    )
    $urlRouterProvider.otherwise('/app/home');
});

controller.js

angular.module('starter.controllers', []) .controller('AppCtrl',function($scope) {}) .controller('homeCtrl',function($scope, $stateParams) {}).controller("placesCtrl", function($scope, $cordovaSQLite) {$scope.categories = [];
$scope.refresh = function() {
    var query = "SELECT Place_Name, Place_Detail FROM Places_of_interest ORDER BY Place_Name ASC";
    $cordovaSQLite.execute(db, query, []).then(function(res) {
        if(res.rows.length > 0) {
            for(var i = 0; i < res.rows.length; i++) {
                $scope.categories.push({place_Name: res.rows.item(i).place_Name, Place_Detail: res.rows.item(i).Place_Detail});
                console.log("SELECTED -> " + res.rows.item(i).Place_Name + " " + res.rows.item(i).Place_Detail);
            }
        } else {
            console.log("No results found");
        }
    }, function (err) {
        console.error(err);
    });
}});

places.html

<ion-view view-title="Places of Interest"><ion-content ng-controller="placesCtrl"><ion-list>
   <button class="button button-small button-balanced"ng-click="refresh()" >
      Referesh
    </button>
   <ion-item ng-repeat="category in categories">
       {{category.Place_Name}} {{category.Place_Detail}} 
   </ion-item>
 </ion-list>

Aucun commentaire:

Enregistrer un commentaire