Search code examples
sqliteionic-frameworkionic2cordova-plugins

Ionic 2: Unable to create table TypeError: Cannot read property 'apply' of undefined


I am trying to open a database and create a table in my Ionic 2 app. The following method is part of a service and is supposed to open the db and create the table:

initDb() {
    let db = new SQLite();
    db.openDatabase({
        name: "data.db",
        location: "default"
    }).then(() => {
        db.executeSql("CREATE TABLE IF NOT EXISTS people (avatarUrl VARCHAR, firstName VARCHAR, lastName VARCHAR)", []).then((data) => {
            console.log("Table created: ", data);

        }, (error) => {
            console.error("Unable to create table", error);

        })
    }, (error) => {

        console.error("Unable to open database", error);
    });
  }

The method is called in my home page's constructor:

constructor(public platform: Platform, public navCtrl: NavController,  public dbService: DBService) {
     this.platform.ready().then(() => {
       this.dbService.initDb();
     });
  }

I have no idea why I am getting this error (refer to the title). Thanks


Solution

  • Sorry, I could not reproduce this error but build a testapp on my own. This app works with me, despite this is called within ready as well:

    app.component.ts:

    import { Component } from '@angular/core';
    import { Platform } from 'ionic-angular';
    import { StatusBar, Splashscreen, SQLite } from 'ionic-native';
    
    import { TabsPage } from '../pages/tabs/tabs';
    import { DbService } from '../providers/db-service';
    
    @Component({
      templateUrl: 'app.html',
      providers: [DbService]
    })
    export class MyApp {
      rootPage = TabsPage;
    
      constructor(public platform: Platform, public dbService: DbService) {
        platform.ready().then(() => {
          // Okay, so the platform is ready and our plugins are available.
          // Here you can do any higher level native things you might need.
          StatusBar.styleDefault();
          Splashscreen.hide();
          this.dbService.initDb();
        });
      }
    }
    

    I made this service by using this ionic-command:

    ionic g provider DbService
    

    db-service.ts:

    import { Injectable } from '@angular/core';
    import { Http } from '@angular/http';
    import 'rxjs/add/operator/map';
    import { SQLite } from 'ionic-native';
    
    /*
      Generated class for the DbService provider.
    
      See https://angular.io/docs/ts/latest/guide/dependency-injection.html
      for more info on providers and Angular 2 DI.
    */
    @Injectable()
    export class DbService {
    
      constructor(public http: Http) {
        console.log('Hello DbService Provider');
      }
    
      initDb() {
        let db = new SQLite();
        db.openDatabase({
          name: "data.db",
          location: "default"
        }).then(() => {
          db.executeSql("CREATE TABLE IF NOT EXISTS people (avatarUrl VARCHAR, firstName VARCHAR, lastName VARCHAR)", []).then((data) => {
            console.log("Table created: ", data);
    
          }, (error) => {
            console.error("Unable to create table", error);
    
          })
        }, (error) => {
    
          console.error("Unable to open database", error);
        });
      }
    
    }
    

    ionic-version: 2.1.18

    cordova-version 6.0.0

    Hope it helps.