node-mysql2 icon indicating copy to clipboard operation
node-mysql2 copied to clipboard

Missing symbol.iterator on RowDataPacket

Open igortas opened this issue 7 years ago • 3 comments

Hi,

I'm making a refactoring to node.js project from callbacks to async await es7 feature, so in that refactoring stage I was migrating from mysql to mysql2 driver. I'm using mysq2/promise so everything works well with new async await keyword, but I have one small problem. When I'm returning promise<RowDataPacket[]> from select, update or insert i can't use for or loop.

export const selectAsync = async (
  sqlStatement: string,
  params: any[]
): Promise<mysql.RowDataPacket[]> => {
  try {
    const connection = await mysql.createConnection({
      host: config.mysql_credentials.db_host,
      user: config.mysql_credentials.db_user,
      password: config.mysql_credentials.db_password,
      database: config.mysql_credentials.db_database,
      multipleStatements: true
    });

    const [rows] = await connection.query<mysql.RowDataPacket[]>(
      sqlStatement,
      params
    );
    return rows;
  } catch (err) {
    console.log('Select query executing error', err);
    return;
  }
};

Now, when I use this selectAsync for example in some function and when the data are fetched from db I can't loop with for of loop when I execute multiples sql queries at once:

 for (let row of result[0]) {
        // some code
      }

result[0] or result[1] gives error that the RowDataPacket must have a symbol iterator. One workaround is to use forEach, but I don't like to change forEeach everywhere where I have for of loop already from previously when for of loop was working with mysql driver.

BR, Igor

igortas avatar Feb 14 '19 12:02 igortas

though a bit different to your error, I do have plans to add async iterators: https://github.com/sidorares/node-mysql2/pull/822#issuecomment-409415308

api might look similar to this example:

const rows = connection.streamQuery(sqlStatement, params);
for await (const row of rows) {
   console.log(row); 
}

sidorares avatar Feb 14 '19 12:02 sidorares

Hey @sidorares, how is going the plan to give streamQuery into the current MySQL2 API

S0c5 avatar Jun 16 '21 19:06 S0c5

What's the status on this ticket?

gordonwho avatar Apr 08 '24 16:04 gordonwho