1

My Error:

Runtime Error Uncaught (in promise): TypeError: _this.sqlstorage.openDatabase is not a function TypeError: _this.sqlstorage.openDatabase is not a function at http://localhost:8100/build/main.js:67:30 at t.invoke (http://localhost:8100/build/polyfills.js:3:9283) at Object.onInvoke (http://localhost:8100/build/vendor.js:4508:37) at t.invoke (http://localhost:8100/build/polyfills.js:3:9223) at r.run (http://localhost:8100/build/polyfills.js:3:4452) at http://localhost:8100/build/polyfills.js:3:14076 at t.invokeTask (http://localhost:8100/build/polyfills.js:3:9967) at Object.onInvokeTask (http://localhost:8100/build/vendor.js:4499:37) at t.invokeTask (http://localhost:8100/build/polyfills.js:3:9888) at r.runTask (http://localhost:8100/build/polyfills.js:3:5143)

Stack

Error: Uncaught (in promise): TypeError: _this.sqlstorage.openDatabase is not a function TypeError: _this.sqlstorage.openDatabase is not a function at http://localhost:8100/build/main.js:67:30 at t.invoke (http://localhost:8100/build/polyfills.js:3:9283) at Object.onInvoke (http://localhost:8100/build/vendor.js:4508:37) at t.invoke (http://localhost:8100/build/polyfills.js:3:9223) at r.run (http://localhost:8100/build/polyfills.js:3:4452) at http://localhost:8100/build/polyfills.js:3:14076 at t.invokeTask (http://localhost:8100/build/polyfills.js:3:9967) at Object.onInvokeTask (http://localhost:8100/build/vendor.js:4499:37) at t.invokeTask (http://localhost:8100/build/polyfills.js:3:9888) at r.runTask (http://localhost:8100/build/polyfills.js:3:5143) at c (http://localhost:8100/build/polyfills.js:3:13535) at http://localhost:8100/build/polyfills.js:3:14107 at t.invokeTask (http://localhost:8100/build/polyfills.js:3:9967) at Object.onInvokeTask (http://localhost:8100/build/vendor.js:4499:37) at t.invokeTask (http://localhost:8100/build/polyfills.js:3:9888) at r.runTask (http://localhost:8100/build/polyfills.js:3:5143) at o (http://localhost:8100/build/polyfills.js:3:2203) at HTMLDocument.invoke (http://localhost:8100/build/polyfills.js:3:10985)

My Home.ts file:

import { Component } from '@angular/core';
import { NavController,Platform } from 'ionic-angular';
import {SQLite} from "@ionic-native/sqlite";

@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {
    sqlstorage: SQLite;
    items: Array<Object>;

  constructor(public navCtrl: NavController, private platform: Platform) {
     this.platform.ready().then(() => {
            for (var i = 100000 - 1; i >= 0; i--) {
                console.log("test");
                    }
            this.sqlstorage = new SQLite();
            this.sqlstorage.openDatabase({name: "items.db", location: "default"}).then(() => {
                this.createTables();
                this.findAll();
            }, (err) => {
                console.log("!!! ", err);
            });
        });

  }

  public createTables(){
        this.sqlstorage.executeSql(`create table if not exists items(
            reference CHAR(10) PRIMARY KEY,
            name CHAR(30),
            qMin FLOAT,
            qReal FLOAT
        ))`, {});            
    }}

Ionic Version details

Ionic Framework: 3.6.0

Ionic App Scripts: 2.1.3

Angular Core: 4.1.3

Angular Compiler CLI: 4.1.3

Node: 8.2.1

OS Platform: Linux 4.4

Navigator Platform: Linux x86_64

User Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.78 Safari/537.36

I tried:

 npm install --save @ionic-native/sqlite

But did not help.

1
  • Have you found a solution? Commented Aug 14, 2017 at 18:13

2 Answers 2

2

You should inject SQLite into your constructor. And SQLite doesn't seem to have a function called openDatabase(). The documentation says to use the function create(config:SQLiteDatabaseConfig) for creating or opening a database.

...

private db: SQLiteObject;

constructor(private sqlite: SQLite, private platform: Platform) {
    platform.ready().then(() => {
        sqlite.create({
            name: "items.db",
            location: "default"
        })
        .then(db => {
            this.db = db;
            this.createTables();
        }
    });
}

createTables(){
    this.db.executeSql(...);
}

...
Sign up to request clarification or add additional context in comments.

3 Comments

Does it mean, .then(db => { this.db = db; this.createTables(); } will be used to load the current database?
can you show me how you did import SQLite? Because I got an 'SQLite' refers to a value, but is being used as a type here. Did you mean 'typeof SQLite'?
Ok, did found it, I had to import it like this import { SQLite } from '@ionic-native/sqlite/ngx'
0
/* This is sqlite object */
  public database: SQLiteObject;

/* This will notify when platform is ready and database is ready to trasction */
private databaseReady: BehaviorSubject<boolean>;

private options = { name: 'test.db', location: 'default' };

this.databaseReady = new BehaviorSubject(false);
    this.platform.ready().then(() => {
      console.log(this.TAG, 'platform is ready');
      this.sqlite.create(this.options)
      .then((db: SQLiteObject) => {
       this.database = db;
       console.log(this.TAG, this.database);
       this.databaseReady.next(true);
       db.executeSql('select tbl_name from sqlite_master', []).then(data => {
         console.log(this.TAG, data);
         }).catch(e => console.log(e));
      });
    }).catch(e => console.log(e));
   }
  public getDatabaseState() {
    return this.databaseReady.asObservable();
  }

//On your page ts file

this.database.getDatabaseState().subscribe(result => {
      console.log(this.TAG, 'Database State', result);
        if (result) {

        }
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.