Consider this method in an angular service:
select : function(table) {
window.sqlitePlugin.openDatabase({
name: 'smartLab.db',
location: 'default'
}, function success(db) {
var defer = window.Q.defer();
console.dir(defer);
db.executeSql('SELECT * FROM ' + table, [], function success(rows) {
var resp = [];
for(var i = 0; i < rows.rows.length; i++) {
resp.push(rows.rows.item(i));
}
defer.resolve(resp);
}, defer.reject)
}, function error(err) {
})
}
I am calling it from another service in the following way:
DatabaseService.select(TBL_NAME).then(function(rows) {
// logic goes here
});
The error I get is:
Uncaught TypeError: Cannot read property 'then' of undefined
I have never used Q before but am familiar with promises, anything jump out as being inherently wrong? Clearly something is...
You are never returning the promise from the defered object (click here for more)
select : function(table) {
var defer = window.Q.defer();
window.sqlitePlugin.openDatabase({
name: 'smartLab.db',
location: 'default'
}, function success(db) {
console.dir(defer);
db.executeSql('SELECT * FROM ' + table, [], function success(rows) {
var resp = [];
for(var i = 0; i < rows.rows.length; i++) {
resp.push(rows.rows.item(i));
}
defer.resolve(resp);
}, defer.reject)
}, function error(err) {
});
return defer.promise;
}