How to set the sqlite result to a global variable


#1

I have a function that makes a query to sqlite, in a global variable I save the result, but the variable is shown undefined.

public lng:any;

getLanguage(){
this.sqlite.create({
name: ‘wconline.db’,
location: ‘default’
}).then((db: SQLiteObject) => {
db.executeSql(‘CREATE TABLE IF NOT EXISTS language(id INTEGER PRIMARY KEY, language TEXT)’, {})
.then(res => console.log(‘Executed SQL’))
.catch(e => console.log(e));

  db.executeSql('SELECT language FROM language WHERE id=1', {})
  .then(res => {
     this.lng = res.rows.item(0).language;
  })
  .catch(e => console.log(e));
}).catch(e => console.log(e))

}

ionViewDidLoad() {
this.getLanguage();
}

ionViewWillEnter() {
this.getLanguage();
}

Thanks in advanace!


#2

At which point does your variable seem to be undefined? Make sure you are checking it after the async calls as it’s not defined before!

Also, have you tried to log the result of your query and if it actually contains the information you think it does?