簡體   English   中英

鏈接承諾在ionic2原生sqlite中失敗

[英]Chaining promises fails in ionic2 native sqlite

我實現了鏈式promise,但是一個函數執行失敗基本上我有以下返回promise的函數

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

這是我實現的方式

    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));
    });
}

然后添加記錄

    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));
    });
}

刪除項目功能

    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));
    });
}

刪除項已成功執行,但從不執行添加塊,否則會引發錯誤

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

有什么事嗎

當您嘗試添加到表中時,事務已完成,因為this.remove(item)中的回調this.remove(item)將在this.database.transaction調用完成后異步執行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM