Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

openDB don't work in android #598

Open
PterPmnta opened this issue Dec 21, 2016 · 3 comments
Open

openDB don't work in android #598

PterPmnta opened this issue Dec 21, 2016 · 3 comments

Comments

@PterPmnta
Copy link

I try to create a new project, but don't work the plugin, this is the error:

ionic.bundle.js:26794 TypeError: Cannot read property 'openDatabase' of undefined
    at Object.crearDB (app.js:48)
    at new networkCtrl (app.js:30)
    at Object.invoke (ionic.bundle.js:17995)
    at $controllerInit (ionic.bundle.js:23397)
    at nodeLinkFn (ionic.bundle.js:22335)
    at compositeLinkFn (ionic.bundle.js:21703)
    at nodeLinkFn (ionic.bundle.js:22387)
    at compositeLinkFn (ionic.bundle.js:21703)
    at compositeLinkFn (ionic.bundle.js:21706)
    at publicLinkFn (ionic.bundle.js:21583)(anonymous function) 

This is my code

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

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

    });

}

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

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

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

function databaseFtr($cordovaSQLite) {

    return {

        crearDB: function() {

            var db;

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


        }

    }

}

Image show the plugin install

plugin db

@brodycj
Copy link

brodycj commented Dec 21, 2016

The problem is that your app does not wait for the deviceready event before attempting to open a database. This is a common problem with Angular controllers and I already documented this as a pitfall.

I will document the "cannot read property" error as a sign of this pitfall when I get a chance.

@PterPmnta
Copy link
Author

@brodybits Thanks for all, now work perfect, sorry, i will to know if exists one way the create the databse without click function xecute a query

@brodycj
Copy link

brodycj commented Dec 22, 2016

I recommend that you look at the suggestion in ionic-team/ng-cordova#8 (comment) by @romgar:

An another way is to initialize angular dynamically when cordova is ready.

window.ionic.Platform.ready(function() {
   angular.bootstrap(document, ['<your_main_app']);
});

Works like a charm.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants