离子SQLite创建功能在DatabaseProvider中不起作用

时间:2019-05-02 18:04:37

标签: sqlite ionic-framework

我试图用sqlite数据库实现一个移动应用程序。 这是我的DatabaseProvider:

import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';

import { Platform } from 'ionic-angular';
import { SQLitePorter } from '@ionic-native/sqlite-porter/ngx';
import { SQLite, SQLiteObject } from '@ionic-native/sqlite/ngx';
import { Observable } from 'rxjs/Observable';
import { BehaviorSubject } from 'rxjs/Rx';
import { Storage } from '@ionic/storage';

@Injectable()
export class DatabaseProvider {

  private database: SQLiteObject;
  private dbReady: BehaviorSubject<boolean> = new BehaviorSubject(false);
  studies = new BehaviorSubject([]);

  constructor(private plt: Platform, private sqlitePorter: SQLitePorter,
              private sqlite: SQLite, public http: HttpClient, private storage: Storage) {
    this.plt.ready().then(() => {
      console.log(  this.sqlite);
      this.sqlite.create({
        name: 'database.db',
        location: 'default'
      })
      .then((db: SQLiteObject) => {
        console.log(db);
        this.database = db;
        this.storage.get('database_filled').then(val => {
          if(val){
            this.dbReady.next(true);
          }
          else {
            this.fillDatabase();
          }
        });
      });
    });
  }

  fillDatabase(){
    console.log('database');

    this.http.get('assets/database.sql', {responseType: 'text'})
    .subscribe(sql => {
      console.log('database subscribe');
      this.sqlitePorter.importSqlToDb(this.database, sql)
      .then( data => {
        console.log("Database created and filled with data");
        this.dbReady.next(true);
        this.storage.set('database_filled', true);
      })
      .catch(e => console.log(e));
    });
  }

  getDatabaseState(){
    return this.dbReady.asObservable();
  }

}

然后在页面的构造函数中,我使用以下命令调用该函数:

  this.db.getDatabaseState().subscribe(ready => {
      console.log("getDatabaseState ready: ", ready);
      if(ready){
        // ...
      }
    });

但是,如果我在移动设备上运行该应用程序,则会在控制台上获得以下输出:

vendor.js:4373 
Angular is running in the development mode. 
Call enableProdMode() to enable the production mode.

vendor.js:132191 
Ionic Native: deviceready event fired after 2336 ms

main.js:189 
getDatabaseState ready:  false

main.js:815
SQLite {}

vendor.js:2134 
ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'split' of undefined
TypeError: Cannot read property 'split' of undefined
    at get (vendor.js:63523)
    at getPlugin (vendor.js:63555)
    at checkAvailability (vendor.js:76445)
    at vendor.js:76911
    at SQLite.create (vendor.js:76916)
    at main.js:816
    at t.invoke (polyfills.js:3)
    at Object.onInvoke (vendor.js:5445)
    at t.invoke (polyfills.js:3)
    at r.run (polyfills.js:3)
    at c (polyfills.js:3)
    at polyfills.js:3
    at t.invokeTask (polyfills.js:3)
    at Object.onInvokeTask (vendor.js:5436)
    at t.invokeTask (polyfills.js:3)
    at r.runTask (polyfills.js:3)
    at o (polyfills.js:3)
    at e.invokeTask [as invoke] (polyfills.js:3)
    at p (polyfills.js:2)
    at HTMLButtonElement.v (polyfills.js:2)

因此,永远不会到达SQLite.create中的console.log(db)。 您有任何解决办法的想法吗?

0 个答案:

没有答案