简体   繁体   English

链接承诺在ionic2原生sqlite中失败

[英]Chaining promises fails in ionic2 native sqlite

Am implemenmting chained promises but one function fails to be executed Basically i have the following functions which return promises 我实现了链式promise,但是一个函数执行失败基本上我有以下返回promise的函数

1. open database
2. add record // incase record exists remove it
3. remove record called from no 2

This is the way i have implemented 这是我实现的方式

    openDb(): Promise<boolean> {   //opens database
    return new Promise((resolve, reject) => {
        this.sqlite.create({
            name: this.dbname,
            location: this.dblocation
        })
            .then((db: SQLiteObject) => {
                this.database = db;
                resolve(true)
            })
            .catch(e => reject(e.message));
    });
}

Then the add record 然后添加记录

    add(item: string, value: any): Promise<boolean> {
    return new Promise((resolve, reject) => {
        this.openDb()
            .then(() => {
                this.database.transaction(
                    (tx: any) => {
                        tx.executeSql(`
                          CREATE TABLE IF NOT EXISTS '${tbl_name}'  
                                (item_id INTEGER PRIMARY KEY AUTOINCREMENT, item unique, value)` ); //incase tbl not there

                         this.remove(item) //next function which gets executed
                            .then(() => {

                                ///THIS PART IS NEVER EXECUTED
                                tx.executeSql(`
                                INSERT INTO '${tbl_name}' (item, value) VALUES(?, ?)`, [item, value],
                                    (t: any, results: any) => {
                                        console.log("executed successifully");
                                        resolve(true)
                                    },
                                    (t: any, message: any) => {
                                        console.log("Failed to execute");
                                        reject(false)

                                    })
                            })
                            .catch(reject(false))
                    }
                );

            })
            .catch(e => reject(e.essage));
    });
}

The the remove item function 删除项目功能

    remove(item: string): Promise<boolean> {
    return new Promise((resolve, reject) => {
        this.openDb()
            .then(() => {
                this.database.transaction(
                    (tx: any) => {
                         tx.executeSql(`
                              DELETE FROM '${tbl_name}'  WHERE item ='${item}'
                              `, [],
                            (t: any, results: any) => {
                                console.log("executed successifully");
                                resolve(true)
                            },
                            (t: any, message: any) => {
                                console.log("Failed to execute");
                                reject(false)

                            })
                    }
                );

            })
            .catch(e => reject(e.essage));
    });
}

The remove item is executed successifully but the add block is never executed insted an error is thrown 删除项已成功执行,但从不执行添加块,否则会引发错误

InvalidStateError: DOM Exception 11: This transaction is already finalized.
 Transactions are committed after its success or failure handlers are called. 
 If you are using a Promise to handle callbacks, be aware that implementations
   following the A+ standard adhere to run-to-completion semantics
     and so Promise resolution occurs on a subsequent tick and therefore 
   after the transaction commits

What could be wrong? 有什么事吗

当您尝试添加到表中时,事务已完成,因为this.remove(item)中的回调this.remove(item)将在this.database.transaction调用完成后异步执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM