cordovaSQLite plugin SELECT not always being called in ionic app

In my app we are trying to use cordovaSQLite plugin and I’m having some issues with it. This might have something to do with how the code is structured but I’m really new to both angular and Ionic. There really isn’t much documentation on best practices using these plugins :frowning:
In my app.js I open the database, create tables if they don’t exist, and insert some data into the tables. I’ve confirmed that this step works without issue. My main screen has a controller assigned to it. in the init() method I attempt to run my SELECT statement to load data from the database. However, this method doesn’t seem to get called. I’ve stepped through the javascript in my browser debugger and noticed that sometimes when I debug it, the database SELECT calls will complete and load the data. When I run the app normally in an iOS simulator, the data never gets loaded. I suspect this may have to do with some async timing issue but I’m a bit lost as to what the problem could be. Here is the code:

      .controller('GameController', function ( $scope, $ionicModal, $cordovaSQLite) {
$scope.init = function () {

            query = "SELECT title, " +
            "from_email, " +
            "to_email, " +
            "subject, " +
            "description, " +
            "body_text, " +
            "FROM tbl_emails";
            
            var db = $cordovaSQLite.openDB("emails.db");
            
            $cordovaSQLite.execute(db, query).then(function (response) {
                for (var i = 0; i < response.rows.length; i++) {
                    var newEmailModel = new EmailModel();
                    newEmailModel.subject = response.rows.item(i).subject;
                    newEmailModel.frome = response.rows.item(i).from_email;
                    newEmailModel.bodyText = response.rows.item(i).body_text;
                    emails.push(newEmailModel);
                }
                $scope.renderEmail(emails[0]);
            }, function (err) {
                console.log("ERROR WHEN GETTING EMAILS FROM tbl_emails -> " + err.message);
            });
        };
    })

My app.js

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

var db = null;

app.run(function ($ionicPlatform, $cordovaSQLite, $cordovaSplashscreen) {
    $ionicPlatform.ready(function () {
        if (window.cordova && window.cordova.plugins.Keyboard) {
            // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
            // for form inputs)
            cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);

            // Don't remove this line unless you know what you are doing. It stops the viewport
            // from snapping when text inputs are focused. Ionic handles this internally for
            // a much nicer keyboard experience.
            cordova.plugins.Keyboard.disableScroll(true);
        }
        if (window.StatusBar) {
            StatusBar.styleDefault();
        }
        
        
        db = $cordovaSQLite.openDB("emails.db");
        createTables($cordovaSQLite);
        loadTables($cordovaSQLite);
        $cordovaSplashscreen.hide();
        
    });
})


app.config(function ($stateProvider, $urlRouterProvider) {
    $urlRouterProvider.otherwise('/')

    $stateProvider.state('home_screen', {
        url: '/',
        templateUrl: 'templates/home_screen.html',
        controller: 'GameController'
    })
})
1 Like

Try putting the database calls in $ionicPlatform.ready();

1 Like

Ah thank you, this solution is working for me now.

1 Like