gpt4 book ai didi

sqlite - ionic 2 : Unable to create table TypeError: Cannot read property 'apply' of undefined

转载 作者:行者123 更新时间:2023-12-02 13:30:36 24 4
gpt4 key购买 nike

我正在尝试打开数据库并在我的 Ionic 2 应用程序中创建一个表。以下方法是服务的一部分,应该打开数据库并创建表:

initDb() {
let db = new SQLite();
db.openDatabase({
name: "data.db",
location: "default"
}).then(() => {
db.executeSql("CREATE TABLE IF NOT EXISTS people (avatarUrl VARCHAR, firstName VARCHAR, lastName VARCHAR)", []).then((data) => {
console.log("Table created: ", data);

}, (error) => {
console.error("Unable to create table", error);

})
}, (error) => {

console.error("Unable to open database", error);
});
}

该方法在我的主页的构造函数中调用:

constructor(public platform: Platform, public navCtrl: NavController,  public dbService: DBService) {
this.platform.ready().then(() => {
this.dbService.initDb();
});
}

我不知道为什么会收到此错误(请参阅标题)。谢谢

最佳答案

抱歉,我无法重现此错误,但我自己构建了一个测试应用程序。尽管this也在ready中调用,但这个应用程序可以与我一起使用:

app.component.ts:

import { Component } from '@angular/core';
import { Platform } from 'ionic-angular';
import { StatusBar, Splashscreen, SQLite } from 'ionic-native';

import { TabsPage } from '../pages/tabs/tabs';
import { DbService } from '../providers/db-service';

@Component({
templateUrl: 'app.html',
providers: [DbService]
})
export class MyApp {
rootPage = TabsPage;

constructor(public platform: Platform, public dbService: DbService) {
platform.ready().then(() => {
// Okay, so the platform is ready and our plugins are available.
// Here you can do any higher level native things you might need.
StatusBar.styleDefault();
Splashscreen.hide();
this.dbService.initDb();
});
}
}

我使用此 ionic 命令创建了此服务:

ionic g provider DbService

db-service.ts:

import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import 'rxjs/add/operator/map';
import { SQLite } from 'ionic-native';

/*
Generated class for the DbService provider.

See https://angular.io/docs/ts/latest/guide/dependency-injection.html
for more info on providers and Angular 2 DI.
*/
@Injectable()
export class DbService {

constructor(public http: Http) {
console.log('Hello DbService Provider');
}

initDb() {
let db = new SQLite();
db.openDatabase({
name: "data.db",
location: "default"
}).then(() => {
db.executeSql("CREATE TABLE IF NOT EXISTS people (avatarUrl VARCHAR, firstName VARCHAR, lastName VARCHAR)", []).then((data) => {
console.log("Table created: ", data);

}, (error) => {
console.error("Unable to create table", error);

})
}, (error) => {

console.error("Unable to open database", error);
});
}

}

ionic 版本:2.1.18

cordova-版本 6.0.0

希望有帮助。

关于sqlite - ionic 2 : Unable to create table TypeError: Cannot read property 'apply' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41347793/

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