Cordova SQLite:数据库已经打开:data.db

时间:2016-10-21 06:21:20

标签: sqlite cordova ionic2

我正在尝试在Ionic 2中实现聊天应用,我使用SQLite。

但是,当我在数据库上执行executeSql时,我收到以下错误。

  

TypeError {stack:(...),message:"无法读取属性' executeSql'   未定义"}

但是当我每次交易打开数据库时,我都没有收到上述错误,但收到以下警告:

  

数据库已经打开:data.db

我一直在阅读IonicCordova文档,但似乎无法理解我做错了什么。如果有人可以提供建议,我将不胜感激。

chatsStorageService.ts

public openDatabase(): Promise<Array<Message>> {
    let promise: Promise<Array<Message>> = new Promise<Array<Message>>(resolve => {
        console.log('openDatabase: ', this.database);
        if (this.database && this.database != null) {
            return Promise.all([this.refreshChats(this.database), this.refreshMessages(this.database)]).then(() => {
                resolve(this.messages);
            });
        } else {
            this.database = new SQLite();
            this.database.openDatabase({
                name: "data.db",
                location: "default"
            }).then(() => {
                return Promise.all([this.refreshChats(this.database), this.refreshMessages(this.database)]).then(() => {
                    resolve(this.messages);
                });
            }, (error) => {
                console.log("OPEN ERROR: ", error);
            });
        }
    });
    return promise;
}

public refreshChats(db: any): Promise<Array<Chat>> {
    let promise: Promise<Array<Chat>> = new Promise<Array<Chat>>(resolve => {
        return db.executeSql("SELECT * FROM chats", [])
            .then((chatData) => {
                let promises: Array<any> = [];
                this.chats = [];
                if (chatData.rows.length > 0) {
                    for (var i = 0; i < chatData.rows.length; i++) {
                        promises.push(this.populateChat(db, chatData.rows.item(i)));
                    }
                }
                return Promise.all(promises).then(() => {
                    resolve(this.chats);
                });
            })
            .catch(error => {
                console.log("ERROR REFRESHING CHATS: " + JSON.stringify(error));
                console.log(error);
            });
    });
    return promise;
}

0 个答案:

没有答案