使用带有Ionic的ngCordova $ cordovaSQLite插件时出错 [英] Errors using the ngCordova $cordovaSQLite plugin with Ionic

查看:263
本文介绍了使用带有Ionic的ngCordova $ cordovaSQLite插件时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试使用我的应用程序实现ngCordova SQLite插件,但尚未生成有效的解决方案。我已经关注了 Nic Raboy的博文关于如何使用Ionic项目将SQLite插件实现为T,但我仍然收到错误: 错误:undefined不是对象(评估'$ window.sqlitePlugin.openDatabase') 当我尝试在iOS模拟器中运行应用程序时。

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.

我还验证了ngCordova和插件已加载到我的项目中。

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>

我的代码如下。

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.

推荐答案

就像Nic Raboy所说的那样,ngCordova SQLite openDB调用的语法已经改变,尽管文档是什么已经说过,以下内容:

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

第二个参数 databaseType ,是数据库的存储方式。在我的情况下,我使用数字用于将数据库存储在隐藏在iTunes中的背景中,但是使用iCloud备份。

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.

注意:如果您尝试在浏览器中工作,此打开的数据库调用将会出错。要解决此问题,您需要引入if语句以根据您的环境更改调用。以下对我有用:

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

这篇关于使用带有Ionic的ngCordova $ cordovaSQLite插件时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆