I created a project in Ionic Creator and downloaded it local to implement offline capabilities with SQLite.
ERROR: TypeError: Cannot read property ‘openDatabase’ of undefined on the line with window.sqlitePlugin.openDatabase in the controller.
In my controller:
angular.module(‘app.controllers’, [‘ionic’])
.controller(‘berichtenCtrl’, [’$scope’, ‘$stateParams’,’$ionicPlatform’, ‘Message’,‘messageService’,’$cordovaSQLite’,’$ionicHistory’,
function ($scope,$stateParams,$ionicPlatform, Message, messageService,$cordovaSQLite,$ionicHistory) {
console.log(‘in controller’);
$scope.$on("$ionicView.enter", function () {
$ionicHistory.clearCache();
$ionicHistory.clearHistory();
});
$scope.messages = [];
console.log('loaddata start');
db = window.sqlitePlugin.openDatabase({name: "test.db", location:'default'});
var query=" SELECT * FROM messages" ;
db.executeSql(query ,[],function(result) {
if(result.rows.length >0){
for(var i=0; i<result.rows.length; i++){
$scope.messages.push(result.rows.item(i));
console.log('XXX mess:'+result.rows.item(i).id);
}
}
else{
console.log("XXX####console######## NO results found #######"+"Table record #: ");
}
}, function(tx, error) {
console.log("ERROR ");
});
$scope.showDelete = false;
$scope.toggleDelete = function(){
$scope.showDelete = !$scope.showDelete;
}
$scope.deleteItem = function($index){
Message.delete($scope.messages[$index].id).then(function(){
$scope.messages.splice($index-1, 1);
})
}
}])
IN services.js:
.run(function($ionicPlatform,$cordovaSQLite,$http,$interval){
console.log(‘RUN STARTED’);
console.log('RUN: before ready');
$ionicPlatform.ready(function () {
console.log('RUN: ready');
//get and store users
db = window.sqlitePlugin.openDatabase({name: "test.db", location:'default'});
var api_url = 'https://testsite.ie/mobile/getUsers/5a1c472e489de0.87865504';
db.executeSql('DROP table users');
db.executeSql('CREATE TABLE IF NOT EXISTS users (id, fname, nr,picture,type,userfunction,userids,users)');
return $http.get(api_url).then(function(resp){
console.log(resp);
try{
console.log('setUsers: in try');
db.transaction(function(tx) {
console.log('setUsers: in transaction ');
angular.forEach(resp.data.data.allusers, function(val) {
console.log('setUsers: foreach');
tx.executeSql("INSERT into users (id, fname, nr,picture,type,userfunction,userids,users)"+
" VALUES (?,?,?,?,?,?,?,?)",
[val.id,val.fname ,val.nr,val.picture,val.type,val.userfunction ,val.userids ,val.users
],null,errorHandler);
});
});
}
finally{
console.log('FINALLY');
}
console.log('END HTTP.GET');
});
});
})
Thanks in advance for your help!