I am developing the mobile hybrid app based on ionic framework and angular js and ngCordova. And I m new to it. I m using SQLite plugin for manipulating the database.
I have following code
service.js
angular.module('imrapp.services', ['imrapp.config'])
.factory('DB', function(DB_CONFIG, $cordovaSQLite) {
var self = this;
self.db = null;
self.firstRunTest = function() {
if (window.cordova) {
self.db = cordovaSQLite.openDB({
name: DB_CONFIG.name
}); //device
} else {
self.db = window.openDatabase(DB_CONFIG.name, '1.0', 'database', -1); // browser
}
return $cordovaSQLite.execute(self.db, 'SELECT name FROM sqlite_master WHERE type=(?) AND name=(?)', ['table', 'sync_table'])
.then(function(result) {
if (result.rows.length > 0) {
console.log(result.rows.item(0).name);
return result.rows.item(0).name;
} else {
return "No results found";
}
}, function(err) {
alert(err);
console.error(err);
});
}
return self;
})
controllers.js
angular.module('imrapp.controllers', [])
.controller('LoginCtrl', function($scope, DB) { //AuthInterceptor,
$scope.data = {};
$scope.login = function() {
var firstRun = DB.firstRunTest();
console.warn("IS FIRST RUN 2 = " + JSON.stringify(firstRun));
}
});
When I call DB.firstRunTest
, from controller. the expected return is the value of result.rows.item(0).name
but it returns the object {"$$state":{"status":0}}
.
The query runs perfectly fine because in method DB.firstRunTest(),
console.log(result.rows.item(0).name);
gives intended value.
Anybody help me on this, where I m going wrong. Thanks