Can not read property 'openDatabase' of undefined on android

1

Trying to manage a database on the device through the plugin that facilitates ngCordova , I have come across the following error, and I do not sincerely see the reason. The device has android 4.2.2

Error

TypeError: Cannot read property 'openDatabase' of undefined

Module

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

Home 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'
            });


        }

    }

}

Screenshot showing the plugin installed in the project

    
asked by Pedro Miguel Pimienta Morales 21.12.2016 в 04:20
source

2 answers

1

I was able to solve the problem by searching directly in the plugin repository in GitHub . The problem is that you can not create and open the database at the same time you run the application, since then, when reading the method of opening the DB which in this case is openDB , this will not be recognized despite having the plugin installed.

The way I solve the problem that is presented to me is, just create and open the database through the use of a click event, as will be explained in the following code.

Code

function networkCtrl($scope, databaseFtr) {

    $scope.dbProcesos = {

        dbCreate: function() {
            var db;
            db = databaseFtr.crearDB();
        }

}




function databaseFtr($cordovaSQLite) {

    return {

        crearDB: function() {

            var db;

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

            return db;

        }
    }
}
    
answered by 22.12.2016 / 23:17
source
0

Try reinstalling the plugin and typing the following command in the terminal to refresh the plugins:

ionic platform update android

You already tell us!

    
answered by 10.01.2017 в 14:50