cordovaSQLite not working, Error: undefined is not an object (evaluating 'n.transaction')

cordovaSQLite not working, Error: undefined is not an object (evaluating 'n.transaction')

这是我确定导致问题的代码部分:

angular.module('starter.controllers', []).controller('StudentsCtrl', function ($scope, $cordovaSQLite) {

  var query = "SELECT id, student_id, username FROM students";
  var users = [];

  $cordovaSQLite.execute(db, query).then(function (data) {
    $.each(data, function(i, item) {
      users.push(item);
    });
  });

  $scope.users = users;
});

我已经包含了 $cordovaSQLite 工作所需的文件。上面的代码在 www/js/controllers.js 中,作为默认 Ionic 选项卡式项目的一部分。 $cordovaSQLitewww/js/app.js 中工作正常,在 www/js/controllers.js 的另一部分也工作正常,但是上面的代码部分 returns 我遇到了这个错误:

0     610246   error    Error: undefined is not an object (evaluating 'n.transaction')

更新: 在此函数中,$cordovaSQLite 似乎等于 undefined,但我不确定为什么会这样。

您的代码 运行 在 Cordova deviceready 事件触发之前。在使用任何设备功能(如插件)之前,您需要等待它。在 Ionic 中,如果你在你的控制器中使用 $ionicPlatform,你可以这样做:

$ionicPlatform.ready(function() {

作为包装需要等待的代码的一种方式。试试这个:

angular.module('starter.controllers', []).controller('StudentsCtrl,$ionicPlatform', function ($scope, $cordovaSQLite) {

  $ionicPlatform.ready(function() {
    var query = "SELECT id, student_id, username FROM students";
    var users = [];

    $cordovaSQLite.execute(db, query).then(function (data) {
      $.each(data, function(i, item) {
        users.push(item);
      });
    });

    $scope.users = users;
    
   });
  
});