Updating multiple rows with node-mysql, NodeJS and Q

前端 未结 5 897
-上瘾入骨i
-上瘾入骨i 2020-12-16 22:44

I am using node-mysql, node-js, and Q promises.

I have successfully updated, deleted, and inserted single rows using the above. As well as inserted multiple rows in

5条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-16 23:06

    Don't know if this problem it's still relevant but I will give my 2 cents:

    The solution that I found is to use Promise.all

    Basically you need to build an array of promises, where every promise is an update statement:

    const getUpdatePromise = (row) => {
      return new Promise( (resolve, reject) => {
        mysqlConnection.query('UPDATE tableName SET foo = ? WHERE bar = ?', [row[0], row[1], (error, results, fields) => {
          if (error) reject(error)
          resolve(results)
        });
      })
    }

    You add this new Promise into an array and pass the final array with all the update statements to the Promise.all function.

    It worked for me. Hope it works for someone else.

提交回复
热议问题