I have a local SQL table called Idioms and a table called Favorites. Favorites only has one column, an idiom_id column. I can check and see if a user has favorited an idiom by running a query like so.
this.sqlite.create({
name: "ionicdb.db",
location: "default"
}).then((db: SQLiteObject) => {
database.executeSql("SELECT * FROM Favorites WHERE idiom_id=?", [idiom_id])
.then(res => {
return res.rows.length > 0 ? true : false
})
})
I’ll be using this a lot so I want to move it into a service, but I’m not quite sure what I should return because the sql query returns a promise.
Doing something like this
export class FavoritesService {
isFavorited(idiom_id, database) {
database.executeSql("SELECT * FROM Favorites WHERE idiom_id=?", [idiom_id])
.then(res => {
return res.rows.length > 0 ? true : false
})
}
}
I’m getting an error when I assign something to the return value like so (in my page.ts file)
this.alertCtrl.create({
title: "SQL",
subTitle: isFavorited(idiom_id, database) .toString()
}).present()
The error I get is
Property 'toString' does not exist on type 'void'
Is this because the SQL query is running asynchronously? If so, do I need to return a promise in my function, instead of the current way?