Ionic2/Angular2在查询之前等待SQLite数据库打开

Ionic2/Angular2在查询之前等待SQLite数据库打开,sqlite,angular,ionic-framework,ionic2,Sqlite,Angular,Ionic Framework,Ionic2,我正在使用离子2(Angular 2)作为混合应用程序。我将一个共享提供程序注入页面,该页面将显示来自SQLite3数据库的数据,然后继续加载数据。但是,在创建我的数据库提供程序时,打开数据库需要一些时间(很少)。但是,我的代码(截至目前)在查询之前并没有等待数据库打开,这显然会导致错误 如何构造代码,使其等待数据库打开以避免崩溃 我的数据库提供程序的构造函数: constructor(private platform: Platform) { this.platform.ready().the

我正在使用离子2(Angular 2)作为混合应用程序。我将一个共享提供程序注入页面,该页面将显示来自SQLite3数据库的数据,然后继续加载数据。但是,在创建我的数据库提供程序时,打开数据库需要一些时间(很少)。但是,我的代码(截至目前)在查询之前并没有等待数据库打开,这显然会导致错误

如何构造代码,使其等待数据库打开以避免崩溃

我的数据库提供程序的构造函数:

constructor(private platform: Platform) {
this.platform.ready().then(() => {
  if(this.isOpen !== true) {
    this.storage = new SQLite();

    this.storage.openDatabase({name: "data.db", location: "default"}).then(() => {
      this.isOpen = true;
      this.storage.executeSql("CREATE TABLE IF NOT EXISTS people (id INTEGER PRIMARY KEY AUTOINCREMENT, firstname TEXT, lastname TEXT)", []);
    });
  }
});
console.log('Hello Database Provider');
此提供程序被注入到我的页面的构造函数中

加载页面(主页)时,它会触发一个调用
load()
函数的事件

ionViewDidLoad() {
this.load();
console.log('Hello Home Page'); 
加载功能:

public load() {
this.database.getPeople().then((result) => {
  this.itemList = <Array<Object>> result;
}, (error) => {
  console.log("LOAD ERROR: ", error);
});
公共加载(){
this.database.getPeople().then((结果)=>{
this.itemList=结果;
},(错误)=>{
日志(“加载错误:”,错误);
});

我非常希望有人能给我指出正确的方向:)

我终于找到了解决问题的办法

首先,我在提供程序中添加了一个函数,用于检查数据库是否已加载,如果未加载,则继续加载:

public openSQLiteDatabase() {
return new Promise((resolve, reject) => {
  if(this.isOpen) {
    console.log("DB IS OPEN");
    resolve(this.isOpen);
  }

  else {
    console.log("DB IS NOT OPEN");
    this.platform.ready().then(() => {
      this.storage.openDatabase({name: "data.db", location: "default"}).then(() => {
        this.appsettings.openSQLiteDatabase().then(() => {
          this.appsettings.getSettings().then((result) => {
            let settings: Settings = <Settings> result;
            this.selectedDataset = settings.selectedDataset;
            this.isOpen = true;
            resolve(this.isOpen);
          });
        });
      }, (error) => {
        reject(error);
      });
    });
  }
});}

有了这样的代码,在打开数据库之前,我从来没有遇到过查询问题!

这里有什么消息吗?我面临着同样的问题。@Demoritz我已经解决了这个问题,并添加了我的解决方案作为答案:)
ionViewDidLoad() {
this.database.openSQLiteDatabase().then(() => {
  this.loadDictionary();
});}