cordovaSQLite无法正常工作,错误:undefined不是对象(评估'n.transaction') [英] cordovaSQLite not working, Error: undefined is not an object (evaluating 'n.transaction')
问题描述
这是我确定导致问题的代码部分:
This is the section of code I have pinpointed to be causing the problem:
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选项卡项目的一部分。 $ cordovaSQLite
在 www / js / app.js
内工作正常,并且在<$ c的另一部分也可以正常工作$ c> www / js / controllers.js ,但上面的代码部分给我发回了这个错误:
I have included the necessary files for $cordovaSQLite
to work. The above code is inside of www/js/controllers.js
, as part of the default Ionic tabbed project. $cordovaSQLite
works fine inside of www/js/app.js
and also works fine in another section of www/js/controllers.js
, but the above section of code returns me this error:
0 610246 error Error: undefined is not an object (evaluating 'n.transaction')
更新:此功能中似乎 $ cordovaSQLite
等于 undefined
,但我不确定为什么会发生这种情况。
Update: It appears that $cordovaSQLite
is equal to undefined
here in this function, but I'm not really sure why this is happening.
推荐答案
您的代码在Cordova deviceready事件被触发之前运行。在使用任何设备功能(如插件)之前,您需要等待它。在Ionic中,如果你在控制器中使用$ ionicPlatform,你可以这样做:
Your code is running before the Cordova deviceready event has fired. You need to wait for it before using any device functions, like plugins. In Ionic, if you use $ionicPlatform in your controller, you can do:
$ ionicPlatform.ready(function(){
作为一种包装需要等待的代码的方法。试试这个:
as a way to wrap code that needs to wait for it. Try this:
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;
});
});
这篇关于cordovaSQLite无法正常工作,错误:undefined不是对象(评估'n.transaction')的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!