Hi, in my app, iam loading data from db like this :
Database.ts
getShiftsFromShifts():Promise<string>{
let shiftsString;
return this.ready.then(()=>this.storage.executeSql('SELECT shifts FROM shifts',{}))
.then((data)=>{
console.log(data);
return data.rows.length>0?data.rows.item(0).shifts:'';
});
}
ShiftService.ts
public getShiftsFromDb():Promise<Array<any>>{
return this.db.getShiftsFromShifts().then((shiftsString)=>{
if(shiftsString==''){
return null;
}
console.log(shiftsString);
let shiftsArray=JSON.parse(shiftsString);
shiftsArray.forEach((shift)=>{
shift.date=new Date(shift.date);
});
return shiftsArray;
});
};
Home.ts
getDataDb(){
this.shiftService.getShiftsFromDb().then((shiftsArray)=>{
this.shifts=shiftsArray;
this.updateShiftsInACalendar(shiftsArray);
});
}
Problem is, how do i handle cases, when for example there are no data in db? Now i do it with if in shiftService.ts method and again with if in Home.ts method. But it seems kinda redundant to me. Is there any proper, more elegant way to handle this cases?
Another example can be, when i request some data whith http request, i get response, but there are no data, how do u handle this cases? Do i have to if everything? Any Ideas?
Should it be done via promise reject?