我是离子1的初学者。我在离子创建器中创建了一个app原型并将其导入我的电脑。现在我想用它添加sqlite数据库。但是,当我尝试实现它时,控制台显示以下错误:
ionic.bundle.js:26799 TypeError: Cannot read property 'execute' of undefined
at ChildScope.$scope.insert (controllers.js:18)
at fn (eval at compile (ionic.bundle.js:27643), <anonymous>:4:209)
at ionic.bundle.js:65429
at ChildScope.$eval (ionic.bundle.js:30400)
at ChildScope.$apply (ionic.bundle.js:30500)
at HTMLButtonElement.<anonymous> (ionic.bundle.js:65428)
at defaultHandlerWrapper (ionic.bundle.js:16792)
at HTMLButtonElement.eventHandler (ionic.bundle.js:16780)
at triggerMouseEvent (ionic.bundle.js:2953)
at tapClick (ionic.bundle.js:2942)
我有一个单独的控制器页面。这是我的控制器代码片段。
angular.module('app.controllers', ['ngCordova'])
.controller('salesCtrl', ['$scope', '$stateParams', // The following is the constructor function for this page's controller. See https://docs.angularjs.org/guide/controller
// You can include any angular dependencies as parameters for this function
// TIP: Access Route Parameters for your page via $stateParams.parameterName
function ($scope, $stateParams,$cordovaSQLite) {
if (window.cordova) {
db = $cordovaSQLite.openDB({ name: "pos.db" }); //device
console.log("not in browser");
}else{
db = window.openDatabase("pos.db", '1', 'my', 1024 * 1024 * 100); // browser
console.log("browser");
}
$scope.insert=function(){
var query="INSERT into items(firstname,lastname) VALUES(?,?)";
$cordovaSQLite.execute(db,query,["firstname","lastname"]).then(function(result){
console.log("INSERT ID:"+result.insertId);
},function(error){
console.log(error);
})
}
}])
这是我的app.js代码段:
.run(function($ionicPlatform,$cordovaSQLite) {
$ionicPlatform.ready(function() {
// Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
// for form inputs)
if (window.cordova && window.cordova.plugins && window.cordova.plugins.Keyboard) {
cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
cordova.plugins.Keyboard.disableScroll(true);
}
if (window.StatusBar) {
// org.apache.cordova.statusbar required
StatusBar.styleDefault();
}
// DATABASE SECTION STARTS
if (window.cordova) {
db = $cordovaSQLite.openDB({ name: "pos.db" }); //device
console.log("not in browser");
}else{
db = window.openDatabase("pos.db", '1', 'my', 1024 * 1024 * 100); // browser
console.log("browser");
}
$cordovaSQLite.execute(db,"CREATE TABLE IF NOT EXISTS items(id integer primary key,firstname text,lastname text )");
//DATABASE SECTION ENDS
// db=$cordovaSQLite.openDB({name:"pos.db"});
});
})
答案 0 :(得分:0)
以正确的方式将$cordovaSQLite
注入您的控制器,您应该没问题:
.controller('salesCtrl', [
'$scope',
'$stateParams',
'$cordovaSQLite', // <- this was missing
function ($scope, $stateParams, $cordovaSQLite) {
//put all your logic here
});