I am new to Angular and struggling to understand a function in my service. I have this code snippet:
checkRoomNameStatus: function()
{
var promises = [];
var emptyRooms = [];
DatabaseService.openDB().transaction(function(tx) {
tx.executeSql("SELECT * FROM report_rooms", [], function(a,b){
for(i = 0; i < b.rows.length; i++){
console.log('B', b.rows.item(i));
if(b.rows.item(i).name == '' || b.rows.item(i).name == null){
emptyRooms.push(b.rows.item(i));
}
promises.push(b.rows.item(i).id);
}
});
});
return $q.all(promises).then(function(){
return emptyRooms;
});
},
What I need help with:
The function is supposed to create a list of rooms without names, and I must wait for the loop to finish before proceeding. However, when I call this function in my controller like:
console.log(SyncService.checkRoomNameStatus());
I get an object as output, but I'm unsure how to access the value array within it. I've tried several methods without success:
console.log('1',SyncService.checkRoomNameStatus());
console.log('2',SyncService.checkRoomNameStatus().$$state.value);
console.log('3',SyncService.checkRoomNameStatus().value);
console.log('4',SyncService.checkRoomNameStatus().value());
SyncService.checkRoomNameStatus().then(function(value){
console.log(value);
})
If anyone could help me understand where I am going wrong and how to correctly handle this situation, I would greatly appreciate it!
Thank you for your assistance!