Search code examples
androidsqlitecordovaionic-frameworkngcordova

Cannot read property 'openDatabase' of undefined in android


Trying to use the plugin to manage database on the mobile device, SQLite, through the ngCordova implementation, but it shows me the same error every time I try to create the database. The phone have Android 4.2.2

TypeError: Cannot read property 'openDatabase' of undefined

My code, add the the ionic and ngCodova for using the plugin's

angular.module('starter', ['ionic', 'ngCordova'])
    .run(startApp)
    .controller('networkCtrl', networkCtrl)
    .factory('databaseFtr', databaseFtr);

Start App

startApp.$inject = ['$ionicPlatform'];

function startApp($ionicPlatform) {

    $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();
        }

    });

}

Controller

networkCtrl.$inject = ['$scope', 'databaseFtr'];

function networkCtrl($scope, databaseFtr) {
    databaseFtr.crearDB();
}

Factory

databaseFtr.$inject = ['$cordovaSQLite'];

function databaseFtr($cordovaSQLite) {

    return {

        crearDB: function() {

            var db;

            db = $cordovaSQLite.openDB({
                name: "mydata.db",
                location: 'default'
            });


        }

    }

}

Here is the capture showing the plugin install

plugin install


Solution

  • You can try to use

     window.sqlitePlugin.openDB({
                name: "mydata.db",
                location: 'default'
            });
    

    Instead of

    $cordovaSQLite.openDB({
                name: "mydata.db",
                location: 'default'
            });