sidorares / sidorares/node-mysql2

Missing symbol.iterator on RowDataPacket

Open
#918 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
4.4k
Forks
680
Avg merge
9h 7m
Merged PRs (30d)
59

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the reported behavior through selectAsync, connection.query, and the multiple-query result represented by RowDataPacket[]. Inspect how the promise query result is represented and verify the fix by confirming that result[0] and result[1] can be consumed with for-of loops without changing callers.

Written by the indexing model from the issue text.

Assessment

Tech stack
node.js, typescript
Domain
backend, database
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.