获取错误属性'openDatabase'在ionic3项目中的类型'SQLite'上不存在?

时间:2017-08-13 10:15:36

标签: sqlite ionic-framework ionic2 ionic3

我的错误:

  

运行时错误未捕获(在承诺中):TypeError:   _this.sqlstorage.openDatabase不是函数TypeError:_this.sqlstorage.openDatabase不是t.invoke上http://localhost:8100/build/main.js:67:30的函数   (http://localhost:8100/build/polyfills.js:3:9283)在Object.onInvoke上   (http://localhost:8100/build/vendor.js:4508:37)at t.invoke   (http://localhost:8100/build/polyfills.js:3:9223)在r.run   (http://localhost:8100/build/polyfills.js:3:4452)at   http://localhost:8100/build/polyfills.js:3:14076在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

堆栈

  

错误:未捕获(在承诺中):TypeError:_this.sqlstorage.openDatabase   不是函数TypeError:_this.sqlstorage.openDatabase不是   功能       在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)       在r.run(http://localhost:8100/build/polyfills.js:3:4452)       在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)       在r.runTask(http://localhost:8100/build/polyfills.js:3:5143)       在c(http://localhost:8100/build/polyfills.js:3:13535)       在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)       在r.runTask(http://localhost:8100/build/polyfills.js:3:5143)       在o(http://localhost:8100/build/polyfills.js:3:2203)       在HTMLDocument.invoke(http://localhost:8100/build/polyfills.js:3:10985

我的Home.ts文件:

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
        ))`, {});            
    }}

离子版详情

离子框架:3.6.0

Ionic App Scripts:2.1.3

Angular Core:4.1.3

Angular Compiler CLI:4.1.3

节点:8.2.1

OS平台:Linux 4.4

Navigator Platform:Linux x86_64

用户代理:Mozilla / 5.0(X11; Linux x86_64)AppleWebKit / 537.36(KHTML,与Gecko一样)Chrome / 60.0.3112.78 Safari / 537.36

我试过了:

 npm install --save @ionic-native/sqlite

但没有帮助。

2 个答案:

答案 0 :(得分:2)

您应该SQLite注入constructor。并且SQLite似乎没有名为openDatabase()的函数。 documentation表示使用功能create(config:SQLiteDatabaseConfig)创建或打开数据库。

...

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

...

答案 1 :(得分: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) {

        }
});