@Harilall wrote:
I am trying to add database functionality into my application but I keep getting this error Uncaught TypeError: Cannot read property 'openDatabase' of undefined. From what I read this can happen if your code is not run in the device ready function or you're running the app in the browser but I am not doing this. I have run it on chrome inspect and I still get the error, here's the code
var db = null;
.run(function($ionicPlatform, $cordovaSQLite) {
$ionicPlatform.ready(function() {
if(window.cordova && window.cordova.plugins.Keyboard) {
// Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
// for form inputs)
cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);// Don't remove this line unless you know what you are doing. It stops the viewport // from snapping when text inputs are focused. Ionic handles this internally for // a much nicer keyboard experience. cordova.plugins.Keyboard.disableScroll(true); } if(window.StatusBar) { StatusBar.styleDefault(); } db = $cordovaSQLite.openDB({name: "expenseApp.db", bgType: 1 }); $cordovaSQLite.execute(db, "create table if not exists expenses (id integer primary key, date text, detail text, amount real)"); }); });
Here's the function within the controller. NB. I have injected $cordovaSQLite into to main controller. After the SQLite execute function it just jumps to the end.
$scope.saveExpense = function(expenseItem){ if(angular.isDefined(expenseItem) && angular.isDefined(expenseItem.detail) && angular.isDefined(expenseItem.amount)) { var query = "insert into expenses (date, detail, amount) values(?,?,?)"; $cordovaSQLite.execute(db, query, [expenseItem.myDate, expenseItem.detail, expenseItem.amount]) .then(function(res){ if (res.rows.length > 0) { var iAlert = $ionicPopup.alert({ title: 'Item added to database', template: 'expenseItem.date = ' + expenseItem.date + '<br/>' + 'expenseItem.detail = ' + expenseItem.detail + '<br/>' + 'expenseItem.amount = ' + expenseItem.amount, }); } }), function(error){ $ionicPopup.alert({ title: 'Error', template: 'Something went wrong <br/>' + error.message }); } /*console.log(expenseItem); $scope.list.push({ date: $filter('date')($scope.myDate,'yyyy-MM-dd'), detail: expenseItem.detail, amount: $filter('number')(expenseItem.amount,2) }); alert("Item added");*/ $state.reload(); } }
Posts: 1
Participants: 1