科尔多瓦SQLite的:无法读取空的财产“交易” [英] Cordova SQLite: Cannot read property 'transaction' of null

查看:226
本文介绍了科尔多瓦SQLite的:无法读取空的财产“交易”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想的SQLite融入我的离子型应用程序,但我不断收到无法读取空的财产交易,而通过浏览器,远程调试或测试时试图查询DB中的装置上的数据。

I am trying to integrate SQLite into my Ionic app, but I keep getting Cannot read property 'transaction' of null while remote debugging or testing via the browser, when trying to query the DB for data on the device.

所以,我已经分手了我所有的configs,控制器和工厂到单独的文件,所有的功能/模块的组合,所以我注入每个模块的配置文件到主应用程序,然后在配置文件里我注入控制器和工厂。

So I have split up all of my configs, controllers and factories into separate files, all grouped by feature / module, so I inject each module's config file into the main app, and then inside of the config file I inject the controller and factory.

我不知道,我已经构造的应用程序可能会被打破的东西吗?顺便

I am not sure if the way that I've structured the app might be breaking something?

下面我列出所有涉及的相关文件,

Below I've listed all of the relevant files involved,

app.js

app.js

var app = angular.module('app', ['ionic', 'ngCordova', 'app.config', 'login.config']);

app.run(['$rootScope', '$ionicPlatform', 'DB', function($rootScope, $ionicPlatform, DB) {

    $ionicPlatform.ready(function() {

        if (window.cordova && window.cordova.plugins.Keyboard) {
            cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
        }

        if (window.StatusBar) {
            StatusBar.styleDefault();
        }

        if(navigator.splashscreen) {
            navigator.splashscreen.hide();
        }

        DB.init();

    });

}]);

app.config.js

app.config.js

var app = angular.module('app.config', ['app.controller', 'app.factory']);

app.config(function($stateProvider, $urlRouterProvider, $httpProvider) {

    $stateProvider
        .state('app', {
            url: "/app",
            abstract: true,
            templateUrl: "templates/menu.html",
            controller: 'appController'
        });

    $urlRouterProvider.otherwise('/login');

});

app.constant('DB_CONFIG', {
    name: 'database.db',
    tables: [
        {
            name: 'users',
            columns: [
                {name: 'id', type: 'integer primary key'},
                {name: 'username', type: 'text'},
                {name: 'password', type: 'text'},
                {name: 'api_key', type: 'text'},
                {name: 'active', type: 'integer'}
            ]
        }
    ]
});

app.factory.js

app.factory.js

var appFactory = angular.module('app.factory', []);

appFactory.factory('DB', ['$q', '$cordovaSQLite', 'DB_CONFIG', function($q, $cordovaSQLite, DB_CONFIG) {

    var db = null;

    var init = function() {

        db = $cordovaSQLite.openDB({ name: DB_CONFIG.name });
        // I use this when testing in browser db = window.openDatabase(DB_CONFIG.name, '1.0', 'database', -1);

        _.each(DB_CONFIG.tables, function(table) {

            var columns = [];

            _.each(table.columns, function(column) {

                columns.push(column.name + ' ' + column.type);

            });

            var sql = 'CREATE TABLE IF NOT EXISTS ' + table.name + ' (' + columns.join(',') + ')';

            console.log('Table ' + table.name + ' initialized');

            query(sql);

        });

    };

    var query = function(sql, bindings) {

        bindings = typeof bindings !== 'undefined' ? bindings : [];

        return $cordovaSQLite.execute(db, sql, bindings);

    };

    var fetchAll = function(result) {

        var output = [];

        for (var i = 0; i < result.rows.length; i++) {
            output.push(result.rows.item(i));
        }

        return output;
    };

    var fetch = function(result) {
        return result.rows.item(0);
    };

    return {
        init: init,
        query: query,
        fetchAll: fetchAll,
        fetch: fetch
    };
}]);

login.config.js

login.config.js

var loginConfig = angular.module('login.config', ['ionic', 'login.controller']);

loginConfig.config(function($stateProvider) {

    $stateProvider

        .state('login', {
            url: "/login",
            templateUrl: "modules/login/views/login.html",
            controller: 'loginController'
        });

});

login.controller.js

login.controller.js

var loginController = angular.module('login.controller', []);

loginController.controller('loginController', ['$scope', '$state', 'DB', function($scope, $state, DB) {

    $scope.doLogin = function() {

        var username = this.username;
        var password = this.password;

        DB.query('SELECT * FROM users WHERE username = ? and password = ?', [username, password]).then(function(result){
            console.log(result);
        }, function(error){
            console.log(error);
        });

    };

}]);

用户已经被插入到数据库中,有时它的工作原理,而我得到的返回结果,它没有大部分时间,我也得到了上述的错误。

The user has already been inserted into the database, and sometimes it works, and I get the results back, most times it doesn't, and I get the above mentioned error.

我假定这是因为数据库没有及时初始化之前,我查询数据库?

I am assuming this is because the DB is not initialising in time, before I query the database?

如果是这样,是有办法,我可以100%肯定,在我查询它的数据库已加载?

If so, is there a way that I can make 100% sure that the database has loaded before I query it?

推荐答案

我曾经同样的问题删除您sqlLite插件,然后重新添加
问题解决了!

i had same problem remove your sqlLite plugin and add it again problem solved!!!

科尔多瓦插件RM io.litehelpers.cordova.sqlite
科尔多瓦插件添加io.litehelpers.cordova.sqlite

cordova plugin rm io.litehelpers.cordova.sqlite cordova plugin add io.litehelpers.cordova.sqlite

这篇关于科尔多瓦SQLite的:无法读取空的财产“交易”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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