Getting error Property 'openDatabase' does not exist on type 'SQLite' in ionic3 project?

Deadly 提交于 2021-02-10 17:28:36

问题


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:


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(...);
}

...



回答2:


/* 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) {

        }
});


来源:https://stackoverflow.com/questions/45659278/getting-error-property-opendatabase-does-not-exist-on-type-sqlite-in-ionic3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!