简体   繁体   English

为什么语句在数据库事务之前执行?

[英]Why statement executes before db transaction?

in below code, I want to set state domains after all the DB transaction completed in my code, please any one tell me to do that operation perfect.在下面的代码中,我想在我的代码中完成所有数据库事务后设置 state 域,请任何人告诉我完美地完成该操作。

I calling below method to set the new domains array我调用下面的方法来设置新的域数组

setFavorites() {
    var arr = this.state.domains.map((item, index) => {
        new Promise((resolve, reject) => {
            db.transaction((tx) => {
                try {
                    tx.executeSql(
                        'SELECT * FROM table_favorites WHERE id=' + item.product_id, [],
                        (tx, results) => {
                            var len = results.rows.length;
                            if (len > 0) { item.isFavorite = true } else { item.isFavorite = false }
                        }
                    );
                    resolve()
                } catch (error) {
                    reject(error)
                }
            });
        })
        return { ...item }
    })
    this.setState({ domains: arr })
}

I would recommend use Promise.all() and inside the.then use the setState()我建议使用 Promise.all() 并在里面使用。然后使用 setState()

something like:就像是:
EDIT : this.state.domains is array of object, so it should not be updated directly编辑:this.state.domains 是 object 的数组,因此不应直接更新

setFavorites() {
  Promise.all(
      this.state.domains.map(item => {
          return new Promise((resolve, reject) => {
              db.transaction((tx) => {
                  try {
                      tx.executeSql(
                          'SELECT * FROM table_favorites WHERE id=' + item.product_id, [],
                          (tx, results) => {
                              var len = results.rows.length;
                              if (len > 0) {
                                resolve({...item, isFavorite = true})
                              }
                              else {
                                resolve({...item, isFavorite = false})
                              }
                          }
                      );
                      
                  } catch (error) {
                      reject(error)
                  }
              });
          })
      })
  )
  .then(arr => this.setState({ domains: arr })) 
}

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

相关问题 我的日志在 sqlite 数据库事务之后没有执行? 为什么它在数据库事务之前执行? - My log not executed after the sqlite database transaction? why its executes before the db transaction? 在phonegap中的db事务语句之前警告火灾 - alert fire before the db transaction statement in phonegap 为什么循环下面的语句在循环终止之前执行 - why does statement below loop executes before termination of loop 返回语句在ajax响应之前执行 - Return statement executes before ajax response 即使IF条件为假,为什么语句也会在Javascript中执行? - Even if, IF condition is false why statement executes in Javascript? 为什么即使在 alert() 之前调用 preventDefault() 也会执行 alert()? - Why alert() executes even though preventDefault() is called before alert()? 为什么在此代码中for循环在Jquery Selector之前执行? - Why do for loop executes before Jquery Selector in this code? 为什么这个动画不稳定,有时会在执行之前有很长的延迟? - Why is this animation choppy and sometimes have long delay before it executes? 为什么 db.transaction 不能与 indexeddb 一起使用? - Why is db.transaction not working with indexeddb? 为什么变量 len 值在 db 事务中没有改变? - Why variable len value not changing in db transaction?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM