2016-09-26 4 views
0

Im new с IONIC, и мне нужна помощь по использованию ngCordova. Я могу создать базу данных на своих приложениях.js. Но я всегда получаю это сообщение об ошибке при попытке чтобы получить доступ к базе данных на мой контроллер . "TypeError: не удается вызвать метод 'OpenDatabase' неопределенных"SQLite on IONIC "TypeError: не удается вызвать метод openDatabase 'undefined

Ниже мои коды:

Controller.js:

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

.controller('loginCtrl', ['$scope', 'LoginService', '$ionicPopup', '$state','$cordovaSQLite', 
function ($scope,LoginService,$ionicPopup, $state,$cordovaSQLite) { 

$scope.data = {}; 

$scope.login = function() { 
    var db = null; 
    console.log("LOGIN user: " + $scope.data.username + " - PW: " + $scope.data.password); 
    LoginService.loginUser($scope.data.username, $scope.data.password).success(function(data) { 
     db = $cordovaSQLite.openDB("guards.db"); 

         var query = "CREATE TABLE IF NOT EXISTS incident_list (id integer autoincrement primary key, complainant string , defendant string, details string, remarks string,incident_date datetime)"; 
         runQuery(query,[],function(res) { 
          console.log("table created done"); 
         }, function (err) { 
          console.log("table created done"); 
          console.log(err); 
         }); 
     $state.go('guardTrack.home'); 
    }).error(function(data) { 
     var alertPopup = $ionicPopup.alert({ 
      title: 'Login failed!', 
      template: 'Please check your credentials!' 
     }); 
    }); 
}}]) 

ответ

0

Check the plugin usage in ngCordova docs you include the sqlite in your controller in order to use it.

module.controller('MyCtrl', function($scope, $cordovaSQLite) { 
    var db = $cordovaSQLite.openDB({ name: "my.db" }); 

// for opening a background db:

var db = $cordovaSQLite.openDB({ name: "my.db", bgType: 1 }); 
    $scope.execute = function() { 
    var query = "INSERT INTO test_table (data, data_num) VALUES (?,?)"; 
    $cordovaSQLite.execute(db, query, ["test",  100]).then(function(res) { 
     console.log("insertId: " + res.insertId); 
    }, function (err) { 
     console.error(err); 
    }); 
    }; 
}); 

помощь Link

Смежные вопросы