title: SQLiteObject is undefined in ionic 3
body: <p>I'm using ionic-native SQLite database for Ionic application and for testing in browser i'm using WebSQL.
  It's all working fine in browser, but when running application in android devices. it gives me error like <code>Cannot read property 'transaction' of undefined</code>.
  Below is code for reference.</p>
  
  <p>1) DBProvider.ts</p>
  
  <pre><code>import { Platform } from 'ionic-angular';
  import { Injectable } from '@angular/core';
  import { SQLite, SQLiteObject } from '@ionic-native/sqlite';
  
  declare var window: any;
  @Injectable()
  export class DBProvider {
      DB_NAME: string = 'DailySheet.db';
      public websql = null;
      public sqlite: SQLite;
      sqliteobj: any;
      public AppUsers = [];
  
      constructor(public platform: Platform) {
          if (this.platform.is('core')) {
              this.websql = window.openDatabase(this.DB_NAME, "1.0", "Test DB", 2 * 1024 * 1024);
              console.log('Database opened.');
              this.createTable();
          }
  
          this.platform.ready().then(() =&gt; {
              if (!this.platform.is('core')) {
                  this.sqlite.create({ name: this.DB_NAME, location: 'default' })
                      .then((db: SQLiteObject) =&gt; {
                          console.log('Database opened.');
                          this.sqliteobj = db;
                          this.createTable();
                      });
              }
          });
      }
  
      createTable() {
          this.query(`CREATE TABLE IF NOT EXISTS AppUser (
                           UserId INTEGER NOT NULL,
                           MobileNo   TEXT NOT NULL UNIQUE,
                           Email  TEXT,
                           PRIMARY KEY(UserId)
                       )`)
              .then(data =&gt; {
                  console.log('Table created.');
              })
              .catch(err =&gt; {
                  console.error('Unable to create initial storage tables', err.tx, err.err);
              });
      }
  
      getAppUsers(): Promise&lt;any&gt; {
          let query = 'SELECT * FROM AppUser';
          return this.query(query)
              .then(data =&gt; {
                  if (data.res.rows.length &gt; 0) {
                      console.log('Rows found.');
                      return data.res.rows;
                  }
                  else {
                      console.log('No rows found.');
                  }
              });
      }
  
      insertAppUser(): Promise&lt;any&gt; {
          let id = 1;
          let mobileno = '8905606191';
          let email = 'niravparsana94@gmail.com';
  
          return this.query('INSERT INTO AppUser (UserId, MobileNo, Email) VALUES (' + id + ' ,\"' + mobileno + '\" ,\"' + email + '\")')
              .then(data =&gt; {
                  console.log('Insert success.');
                  return data;
              })
              .catch(err =&gt; {
                  console.error('Unable to insert', err.tx, err.err);
              });
      }
  
      updateAppUser(UserId): Promise&lt;any&gt; {
          let query = "UPDATE Todo SET Email=? WHERE UserId=?";
          return this.query(query, ['niravparsana@outlook.com', UserId])
              .then(data =&gt; {
                  console.log('AppUser Updated.');
                  return data;
              })
              .catch(err =&gt; {
                  console.error('Unable to update', err.tx, err.err);
              });
      }
  
      deleteAppUser(UserId): Promise&lt;any&gt; {
          let query = "DELETE FROM AppUser WHERE UserId=?";
          return this.query(query, [UserId])
              .then(data =&gt; {
                  return data;
              })
              .catch(err =&gt; {
                  console.error('Unable to delete', err.tx, err.err);
              });
      }
  
      query(query: string, params: any[] = []): Promise&lt;any&gt; {
          return new Promise((resolve, reject) =&gt; {
              try {
                  if (this.platform.is('core')) {
                      this.websql.transaction((tx: any) =&gt; {
                          tx.executeSql(query, params,
                              (tx: any, res: any) =&gt; resolve({ tx: tx, res: res }),
                              (tx: any, err: any) =&gt; reject({ tx: tx, err: err }));
                      },
                          (err: any) =&gt; reject({ err: err }));
                  }
                  else {
                      this.sqliteobj.transaction((tx: any) =&gt; {
                          tx.executeSql(query, params,
                              (tx: any, res: any) =&gt; resolve({ tx: tx, res: res }),
                              (tx: any, err: any) =&gt; reject({ tx: tx, err: err }));
                      },
                          (err: any) =&gt; reject({ err: err }));
                  }
              } catch (err) {
                  reject({ err: err });
              }
          });
      }
  }
  </code></pre>
  
  <p>2) home.ts</p>
  
  <pre><code>import { Component, OnInit } from '@angular/core';
  import { NavController, Platform } from 'ionic-angular';
  import { DBProvider } from '../../providers/DBProvider';
  
  @Component({
    selector: 'page-home',
    templateUrl: 'home.html'
  })
  export class HomePage implements OnInit {
    AppUsers: Array&lt;Object&gt;;
  
    constructor(public navCtrl: NavController, private platform: Platform, public db: DBProvider) {
    }
  
    ionViewDidLoad() {
      this.deleteAppUser();
      this.insertAppUser();
      this.getAllAppUsers();
    }
  
    ngOnInit() {
    }
  
    public deleteAppUser() {
      this.db.deleteAppUser(1)
        .then(data =&gt; {
          if (data.res.rowsAffected == 1) {
            console.log('AppUser Deleted.');
          }
          else {
            console.log('No AppUser Deleted.');
          }
        })
        .catch(ex =&gt; {
          console.log(ex);
        });
    }
  
    public insertAppUser() {
      this.db.insertAppUser()
        .then(data =&gt; {
        })
        .catch(ex =&gt; {
          console.log(ex);
        });
    }
  
    public getAllAppUsers() {
      this.db.getAppUsers()
        .then(data =&gt; {
          this.AppUsers = data;
        })
        .catch(ex =&gt; {
          console.log(ex);
        });
    }
  
  }
  </code></pre>
  
  <p>While debugging, I figured out somewhat that code runs in difference sequence in browser and mobile.</p>
  
  <h1>In browser</h1>
  
  <ol>
  <li>DBProvider constructor</li>
  <li>this.CreateTable() function(DBProvider.ts)</li>
  <li>this.deleteAppUser() function(home.ts)</li>
  <li>this.insertAppUser() function(home.ts)</li>
  <li>this.getAllAppUsers() function(home.ts)</li>
  </ol>
  
  <h1>In Android device</h1>
  
  <ol>
  <li>DBProvider constructor</li>
  <li>this.deleteAppUser() function(home.ts)</li>
  <li>this.insertAppUser() function(home.ts)</li>
  <li>this.getAllAppUsers() function(home.ts)</li>
  <li>this.CreateTable() function(DBProvider.ts)</li>
  </ol>
  
  <p>As you can this.sqliteobj is assigned in DBProvider constructor. but while debug i found that funtions from home.ts are calling before this.sqliteobj get assigned, that's why it gives an error like <code>Cannot read property 'transaction' of undefined</code>. But then question is why functions from home.ts getting called before this.sqliteobj get assigned?</p>