简体   繁体   中英

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

插件安装

You can try to use

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

Instead of

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

I was able to find a solution to the problem, which was completely unknown.

The reason why this problem is generated is because the creation and opening of the database is intended to originate at the same time as the application tries to start, and since the database can not exist without having started the application , Because the method of creation of this, it is not possible to be executed.

The solution that applies in my case, is that the database was created after running a function through the command ng-click .

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;

        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM