title: TypeError: Cannot read property ‘executeSql’ of undefined
body: <p>I've got a <strong>SQLite provider</strong> ("<em>DatabaseProvider</em>") with some <strong>read and write methods</strong> ("<em>insertActivity</em>" and "<em>getData</em>"). </p>
  
  <p>When I call my <em>insertActivity()</em> method all goes well; 
  but when I call <em>getData()</em> it gives me this error:</p>
  
  <blockquote>
    <p><em>ERROR: TypeError: Cannot read property 'executeSql' of undefined</em></p>
  </blockquote>
  
  <p>I can't understand why, because I think methods are <strong>identical</strong> and .ts files from where I call them, too. </p>
  
  <p>Here's my <strong>database.ts</strong>:</p>
  
  <pre><code>import { Injectable } from '@angular/core';
  import { SQLite, SQLiteObject } from '@ionic-native/sqlite';
  import { Http } from '@angular/http';
  import 'rxjs/add/operator/map';
  
  @Injectable()
  export class DatabaseProvider {
    // private storage:  SQLite;
    public db:       SQLiteObject;
    public isOpen:   boolean;
    public storage:  any;
  
    constructor(
      public http:      Http,
      public database:  SQLite,
  
    ) {
  
      if (!this.isOpen) {
          // create db and tables
  
            this.database = new SQLite();
            this.database.create({name: 'RLSDB', location: 'default'})
            .then((db: SQLiteObject) =&gt; {
              this.storage = db;
              db.executeSql('create table if not exists LOGIN (id INTEGER PRIMARY KEY AUTOINCREMENT, sid TEXT, username TEXT, password TEXT, email TEXT, timestamp TEXT)', {}).then((data) =&gt; {
                this.isOpen = true;
                console.log('TABLE CREATED: ', data);
              }, (error) =&gt; {
                console.error("Unable to execute sql", error);
              });
            }).then(() =&gt; {
              this.storage.executeSql('create table if not exists ACTIVITIES (id INTEGER PRIMARY KEY AUTOINCREMENT, activity_name TEXT, activity_datetime TEXT)', {});
              this.isOpen = true;
            }
          );
      }
    }
  
    public getData() {
  
      return new Promise((resolve, reject) =&gt; {
        this.storage.executeSql('SELECT * from ACTIVITIES', []).then((data) =&gt; {
          let activityValues = [];
          if (data.rows.length &gt; 0) {
            for(let i=0; i &lt;data.rows.length; i++) {
              activityValues.push(data.rows.item(i).activity_datetime);
            }
          }
          return resolve(data);
      }, (error) =&gt; {
        return reject(error);
      });
    });
  
  
    }
  
    public insertActivity(
      RLSActivity: string,
      RLSActivityDatetime: any ) {
        return new Promise((resolve, reject) =&gt; {
          this.storage.executeSql("INSERT INTO ACTIVITIES (activity_name, activity_datetime) VALUES (?, ?)", [RLSActivity, RLSActivityDatetime]).then((data) =&gt; {
            return resolve(data);
        }, (error) =&gt; {
          return reject(error);
        });
      });
    }
  
  }
  </code></pre>
  
  <p>I use it on two pages: "<strong>modal-add</strong>" and "<strong>stats</strong>". </p>
  
  <p>my <strong>modal-add.ts</strong>, <strong>which works perfectly</strong>:</p>
  
  <pre><code>import { Component } from '@angular/core';
  import { NavController  } from 'ionic-angular';
  import { IonicPage, NavParams, ViewController } from 'ionic-angular';
  import { DiaryPage } from '../diary/diary';
  import {Validators, FormBuilder, FormGroup } from '@angular/forms';
  import { DatabaseProvider } from '../../providers/database/database';
  
  @IonicPage()
  @Component({
    selector: 'page-modal-add',
    templateUrl: 'modal-add.html',
  })
  export class ModalAddPage {
  
    private activityForm: FormGroup;
  
    constructor(
      public navParams: NavParams,
      private view: ViewController,
      private navCtrl: NavController,
      private formBuilder: FormBuilder,
      public database: DatabaseProvider
    ) {
      this.activityForm = this.formBuilder.group({
            title: [''],
            date: [''],
          });
  
     }
  
    closeModal(){
  
      const inputData = {
        title: this.activityForm.value.title,
        date: this.activityForm.value.date,
      };
  
      this.database.insertActivity(inputData.title, inputData.date);
    (...)
    }
  
  }
  </code></pre>
  
  <p>and my <strong>stats.ts</strong>, <strong>which returns error</strong>:</p>
  
  <pre><code>import { Component, ViewChild } from '@angular/core';
  import { IonicPage, NavController, NavParams } from 'ionic-angular';
  import { DiaryPage } from '../diary/diary';
  import { DatabaseProvider } from '../../providers/database/database';
  import { Chart } from 'chart.js';
  
  @IonicPage()
  @Component({
    selector: 'page-stats',
    templateUrl: 'stats.html'
  })
  export class StatsPage {
  
    @ViewChild('lineCanvas') lineCanvas;
  
    lineChart: any;
    events: any;
  
    constructor(
      public navCtrl: NavController,
      public navParams: NavParams,
      private db: DatabaseProvider
    ) {
  
    } 
  
    ionViewDidLoad() {
  
      this.events = this.db.getData();
      this.lineChart = new Chart(this.lineCanvas.nativeElement, {
       (...) // stuff to create a chart
    }
  
  }
  </code></pre>
  
  <p>Any ideas? </p>
  
  <p>Thank you in advance for your help.</p>