Code Not Being Executed


#1

I was having problems with the SQLite database but managed to solve that error by placing the code within a deviceready listener function, the problem now is although I don’t get the same prior errors my code in the deviceready function isn’t getting hit. I have tried debugging in Chrome and it just skips over the code - please advise, here’s my function code below

$scope.saveExpense = function(expenseItem){
		if(angular.isDefined(expenseItem) && angular.isDefined(expenseItem.detail) && angular.isDefined(expenseItem.amount))
		{
			var dateInDb = $filter('date')(expenseItem.myDate, 'yyyy-MM-dd');			
			
				document.addEventListener('deviceready', function(){ 
				
					//db = $cordovaSQLite.openDB({name: "expenseApp.db"});
					var query = "insert into expenses (date, detail, amount) values(?,?,?)";
					$cordovaSQLite.execute(db, query, [dateInDb, expenseItem.detail, expenseItem.amount])
					.then(function(res){
						console.log("res = " + 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
						});
					}
				
				});

			$state.reload();
		}
	}

#2
 ionic.Platform.ready(function () {
    ...
});