Errors using the ngCordova $cordovaSQLite plugin w

2019-04-02 06:44发布

I am currently trying to implement the ngCordova SQLite plugin with my app but have yet to produce a working solution. I have followed Nic Raboy's blog post on how to implement the SQLite plugin with your Ionic project to a "T," but I am still receiving the error: Error: undefined is not an object (evaluating '$window.sqlitePlugin.openDatabase') when I try to run the application in the iOS emulator.

I have also verified that ngCordova and the plugin have been loaded into my project.

This is the order of how my scripts are getting loaded into my project:

<script src="lib/ionic/js/ionic.bundle.js"></script>
<script src="lib/ngCordova/dist/ng-cordova.js"></script>
<script src="cordova.js"></script>
<script src="js/app.js"></script>
<script src="js/controllers.js"></script>

My code is below.

app.js

angular.module('whoPaidLast', ['ionic', 'ngCordova', 'whoPaidLast.controllers', 'whoPaidLast.services'])

.run(function($ionicPlatform, $cordovaSQLite) {
    $ionicPlatform.ready(function() {
        if(window.cordova && window.cordova.plugins.Keyboard) {
            cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
        }
        if(window.StatusBar) {
            StatusBar.styleDefault();
        }

        db = $cordovaSQLite.openDB({ name: 'accounts.db' });
        $cordovaSQLite.execute(db, 'CREATE TABLE IF NOT EXISTS accounts (id integer primary key autoincrement, firstname text, lastname text, paid numeric, date text)');
    });
})

controllers.js

angular.module('whoPaidLast.controllers', [])

.controller('DashCtrl', ['$ionicPlatform', '$scope', '$ionicModal', '$ionicActionSheet', '$cordovaSQLite', function ($ionicPlatform, $scope, $ionicModal, $ionicActionSheet, $cordovaSQLite) {

    $scope.getList = function() {
        var query = 'SELECT id, firstname, lastname, paid, date FROM accounts ORDER BY lastname ASC';
        var db = $cordovaSQLite.openDB({ name: 'accounts.db' });

        $ionicPlatform.ready(function() {
            $cordovaSQLite.execute(db, query, [id, firstname, lastname, paid, date]).then(function(result) {
                if(result.rows.length > 0) {
                    console.log('rows =' + result.rows);
                } else {
                    $scope.results = [];
                }
            }, function(error) {
                console.error(err);
            });
        });
    };
});

Any information or help that you may have would help. Thanks ahead of time.

3条回答
够拽才男人
2楼-- · 2019-04-02 06:58

I don't know if you found the answer. I had many issues first than configurate properly project.

  • One problem could be that you should import as last thing ng-cordova.js and then cordova.js. app.js and controller.js should be imported first.

  • If you are running in a web app could have a problem with cordova ready parameter that won't be never ready so database won't be open.

  • Try with new call that Triccum said.

I don't remember more issues.

查看更多
看我几分像从前
3楼-- · 2019-04-02 07:08

Like Nic Raboy stated, the syntax for the ngCordova SQLite openDB calls has changed, despite what the documentation has said, to the following:

db = $cordovaSQLite.openDB( databasename, databaseType );

The second parameter databaseType, is how the database is being stored. In my case I used the number use to store the database in the background hidden from iTunes, but backed-up with iCloud.

By changing the syntax of this call, this fixed my error.

NOTE: if you are trying to work in the browser, this open database call will error. To work around this, you will need to introduce an "if" statement to change out the call depending on your environment. The following worked for me:

if (window.cordova && window.SQLitePlugin) {
    var db = $cordovaSQLite.openDB( 'accounts.db', 1 );
} else {
    db = window.openDatabase('accounts', '1.0', 'accounts.db', 100 * 1024 * 1024);
}
查看更多
在下西门庆
4楼-- · 2019-04-02 07:20

I spent more then half a day to figure the solution out.In my case I updated my project from previous version of ionic to a newer one.But the problem was that I copied www folder but didn't add the sqlite plugin again.

So all I did to fix was :

cordova plugin add cordova-sqlite-storage
查看更多
登录 后发表回答