How Do I Use Deviceready in A function on a Controller with $cordovaSQLite


#1

Guys how do I use the deviceready function in a controller to get access to my SQLite database. I keep getting the error cannot read property null of undefined, here’s my code

$ionicPlatform.ready(function(){ 
		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("then function hit" + 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
						});
					}
				
				});
			});