sidorares / sidorares/node-mysql2

Can't start stream query because query.start() throws error

Open
#2,245 0 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 there! I'm trying to stream results from an sql select, but when I call query.start() it throws the following error:

2023-10-17T00:55:03.668Z	cfca73dc-e40f-46f7-bc83-059c4ff1c6d6	ERROR	Handler error: Cannot read properties of undefined (reading 'config'):  {
  aws_request_id: 'cfca73dc-e40f-46f7-bc83-059c4ff1c6d6',
  function_name: 'test_function',
  stackTrace: "TypeError: Cannot read properties of undefined (reading 'config')\n" +
    '    at Query.start (/var/task/handler.js:64172:25)\n' +
    '    at handler (/var/task/handler.js:69581:9)\n' +
    '    at async Runtime.handler (/var/task/handler.js:69219:12)',

Sorry for the bundle stack trace, it's in a lambda function handler. This is when running locally. Here is the code that calls it:

  const query = connection.query(sql, values);
  const stream = query.stream();
  stream
    .on('error', function (err) {
      console.log('err:', err);
    })
    .on('result', function (row) {
      console.log('row:', row);
    })
    .on('fields', (fields) => {
      console.log('fields:', fields);
    })
    .on('end', function () {
      console.log('ENDED');
    });
  query.start();

I do see all the column definitions in the fields handler. Tracking down the offending code in the bundle, it appears to be the first line in the start() function:

      /* eslint no-unused-vars: ["error", { "argsIgnorePattern": "^_" }] */
      start(_packet, connection2) {
        if (connection2.config.debug) {    <==== HERE =================================
          console.log("        Sending query command: %s", this.sql);
        }
        this._connection = connection2;
        this.options = Object.assign({}, connection2.config, this._queryOptions);
        this._setTimeout();
        const cmdPacket = new Packets.Query(
          this.sql,
          connection2.config.charsetNumber
        );
        connection2.writePacket(cmdPacket.toPacket(1));
        return Query.prototype.resultsetHeader;
      }
      done() {
        this._unpipeStream();
        if (this.timeout && !this.queryTimeout) {
          return null;
        }

I wonder if I'm doing something wrong? Any thoughts?
Thanks in advance! :)

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 at the Query.start entry point shown in the report and trace how the stream() call invokes it. Reproduce the provided connection.query(...).stream() sequence, then compare it with the project's documented streaming usage or relevant tests. Done means the query starts without an undefined connection configuration and streamed rows complete successfully, or the issue is clarified as incorrect usage.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.