Я пытался реализовать мобильное приложение с базой данных 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)
Поэтому console.log (db) в SQLite.create никогда не достигается.У тебя есть идеи как это исправить?