gpt4 book ai didi

angular - SQLite 与 ionic 4?无法读取未定义类型错误 : Cannot read property 'then' of undefined 的属性 'then'

转载 作者:太空狗 更新时间:2023-10-29 18:28:42 25 4
gpt4 key购买 nike

首先我有 this error我解决了它。好的,不错。当我尝试使用我的设备进行调试时(使用 ionic cordova run android)。数据库 SQL 没有加载,它表明没有或从未创建数据库 sqlite。我检查了控制台,我看到了这个:

ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'then' of >undefined TypeError: Cannot read property 'then' of undefined at AppComponent.push../src/app/app.component.ts.AppComponent.createDatabase >(app.component.ts:101) at app.component.ts:87

在 app.component.ts 我有这个:

import { SQLite, SQLiteObject } from '@ionic-native/sqlite/ngx';
import { DbService } from './services/db/db.service';


rootPage: string = null;
constructor(
private platform: Platform,
private splashScreen: SplashScreen,
private statusBar: StatusBar,
private translate: TranslateService,
private router: Router,
private menuCtrl: MenuController,
public DbService: DbService,
public sqlite: SQLite
//private screenOrientation: ScreenOrientation
) {
this.initializeApp();
}

public TableCreated = "POR EL MOMENTO NO";
public ahora = "ahora";

initializeApp() {
this.platform.ready().then(() => {
this.statusBar.styleDefault();
this.splashScreen.hide();
this.translate.setDefaultLang('en');
this.createDatabase();

});
}

closeMenu() {
this.menuCtrl.close();
}

private createDatabase(){
this.sqlite.create({
name: 'data.db',
location: 'default' // the location field is required
}).then((db: SQLiteObject) => { <<<<----- ERROR HERE !!! but I don't know why.
this.TableCreated = "FUE CREADA LA DB!";
this.DbService.setDatabase(db);
return this.DbService.createTable();
})
.then(() => {
this.TableCreated = "FUE CREADA LA TABLA!";
return this.DbService.createFirstRunningApp();
})
.then(() =>{
this.TableCreated = "SE INSERTÓ LOS DATOS";
this.splashScreen.hide();
this.rootPage = 'HomePage';
})
.catch(error =>{
console.error(error);
});
}

在 db.services.ts 中:

import { SQLiteObject } from '@ionic-native/sqlite/ngx';


export class DbService {
db: SQLiteObject = null;

constructor() { }

setDatabase(db: SQLiteObject){
if(this.db === null){
this.db = db;
}
}

createTable(){
let sql = 'CREATE TABLE IF NOT EXISTS datis(id INTEGER PRIMARY KEY AUTOINCREMENT, dati TEXT, isTrue INTEGER DEFAULT 0, dateCreated datetime default CURRENT_TIMESTAMP)';
return this.db.executeSql(sql, []);

}

createFirstRunningApp(){ //solo la primera vez se corre
let allDate = [
'Blablabla',
'Something',
'YellowBlueRed',
];
let sql = 'INSERT INTO datis(dati) VALUES(?)';
for (let dati of allDate) {
this.db.executeSql(sql, [dati]);
}
}
}

this.sqlite.create({ 名称:'data.db', location: 'default'//位置字段是必需的 }).then((db: SQLiteObject) <--- 我认为这是错误,但我不知道为什么。

我的信息:

Ionic:

ionic (Ionic CLI) : 4.1.2 (C:\Users\Tomas\AppData\Roaming\npm\node_m
odules\ionic)
Ionic Framework : @ionic/angular 4.0.0-beta.7
@angular-devkit/core : 0.7.5
@angular-devkit/schematics : 0.7.5
@angular/cli : 6.1.5
@ionic/ng-toolkit : 1.0.8
@ionic/schematics-angular : 1.0.6

Cordova:

cordova (Cordova CLI) : 8.0.0
Cordova Platforms : android 7.0.0
Cordova Plugins : cordova-plugin-ionic-keyboard 2.1.3, cordova-plugin-i
onic-webview 2.1.4, (and 4 other plugins)

System:

NodeJS : v8.12.0 (C:\Program Files\nodejs\node.exe)
npm : 6.4.1
OS : Windows 7

有什么解决办法吗?郑重声明,我是初学者,也许代码中有一些东西逃逸了。

谢谢。

最佳答案

ionic cordova platform add browser

然后

ionic cordova run browser

我必须运行第二个命令。使用第一个命令,因为它在第一个命令后没有为我构建。在此之后,它起作用了。

关于angular - SQLite 与 ionic 4?无法读取未定义类型错误 : Cannot read property 'then' of undefined 的属性 'then',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52505457/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com