I am a beginner in ionic 1. I created an app prototype in ionic creator and imported it on my pc. Now I want to add sqlite database with it. But, while I tried to implement it the console shows below error:
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)
I have a separate controller page. Here's my controller code snippent.
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);
})
}
}])
And here is my app.js code snippet:
.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"});
});
})
Inject $cordovaSQLite
into your controller in the right way and you should be fine:
.controller('salesCtrl', [
'$scope',
'$stateParams',
'$cordovaSQLite', // <- this was missing
function ($scope, $stateParams, $cordovaSQLite) {
//put all your logic here
});