Skip to content

Error "Cannot read property '_getConnection' of undefined" during runtime upgrading to Typescript 4.4 #1408

@deadratfink

Description

@deadratfink

Describe the problem

Hello,

we are using this package in v5.2 in one of our projects with Typescript 4.3 on top of Node.js 14 to access the oracle DB and everything is working fine so far.

One day we upgraded to Typescript 4.4 and received following error during runtime:

{
    "errorType": "TypeError",
    "errorMessage": "Cannot read property '_getConnection' of undefined",
    "awsRequestId": "86de41f3-79d7-48aa-b419-f98eb52a7f6b",
    "stack": [
        "TypeError: Cannot read property '_getConnection' of undefined",
        "    at N (/var/task/av-create-accounts.js:2:1414450)",
        "    at /var/task/av-create-accounts.js:2:1441145",
        "    at t.getOracleConnection (/var/task/av-create-accounts.js:2:1465074)",
        "    at new c (/var/task/av-create-accounts.js:2:1462453)",
        "    at e.getServiceValue (/var/task/av-create-accounts.js:2:1482740)",
        "    at e.get (/var/task/av-create-accounts.js:2:1480527)",
        "    at Function.e.get (/var/task/av-create-accounts.js:2:1484047)",
        "    at Runtime.t.handler (/var/task/av-create-accounts.js:2:1470220)",
        "    at processTicksAndRejections (internal/process/task_queues.js:95:5)"
    ]
}

So we investigated this and drilled it down to the the following piece of code used in our project:

import { Connection, getConnection } from 'oracledb';

export function getOracleConnection(username: string, password: string, connectString: string): Promise<Connection> {
  return getConnection({
    user: username,
    password,
    connectString,
  });
}

without using a connection pool. The exported oracledb package's function getConnection itself refers to a this._getConnection(connAttrs):

async function getConnection(a1) {
  let pool;
  let poolAlias;
  let connAttrs = {};

  // verify the number and types of arguments
  nodbUtil.checkArgCount(arguments, 0, 1);
  if (arguments.length == 0) {
    poolAlias = defaultPoolAlias;
  } else {
    nodbUtil.assert(typeof a1 === 'string' || nodbUtil.isObject(a1),
      'NJS-005', 1);
    if (typeof a1 === 'string') {
      poolAlias = a1;
    } else {
      connAttrs = a1;
      if (connAttrs.poolAlias) {
        poolAlias = connAttrs.poolAlias;
      }
    }
  }

  // if a pool alias is available, acquire a connection from the specified pool
  if (poolAlias) {
    pool = poolCache[poolAlias];
    if (!pool) {
      throw new Error(nodbUtil.getErrorMessage('NJS-047', poolAlias));
    }
    return await pool.getConnection(connAttrs);

  // otherwise, create a new standalone connection
  } else {
    try {
      return await this._getConnection(connAttrs);
    } catch (err) {
      if (err.message.match(/DPI-1047/)) {
        err.message += "\n" + nodbUtil.getInstallHelp();
      }
      throw err;
    }
  }
}

which looks like the _getConnection stated in the error message above. Looks like a this-binding done somewhere, but at least I couldn't find a reference to it in the file.

After some research we found a breaking change in Typescript 4.4 stated in More-Compliant Indirect Calls for Imported Functions which could be a hint that the this is not defined by that change in Typescript.

Have you ever experienced problems with that error? Or have you tested the lib with Typescript 4.4 already? Maybe we are doing something wrong?

At the moment we might see only a workaround using a connection pool so that we do not land in the else branch. Any other hints or suggestions to handle it different would be highly appreciated. Thanks in advance.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions