An-droid
An-droid

Reputation: 6485

Requesting to oracle with nodejs using promises

I'm trying to execute a block function in oracle in nodejs but I get no response and this after 10sec :

UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: Mauvaise requête: Error: ORA-12170: TNS:Connect timeout occurred

In the meantime the code continued to execute. Here is my workflow :

const jwtTokenResponse: any = await authService.userLogin(req.body);

then in userLogin

const result = await oracleHelper.executeBlockFunction(block, bindVars);

and finally the function :

  async executeBlockFunction(block: any, bindVars: any): Promise<any> {

    logger.info(`--> ` + 'executeBlockFunction : ' + config.oracle.user + ' / ');
    logger.info(`--> ` + 'executeBlockFunction : ' + config.oracle.password + ' / ');
    logger.info(`--> ` + 'executeBlockFunction : ' + config.oracle.connectString + ' / ');
    oracledb.getConnection(
      {
        user: config.oracle.user,
        password: config.oracle.password,
        connectString: config.oracle.connectString,
      }).then(function (connection) {
      logger.info(`--> connected`);
      return connection.execute(
        block,
        bindVars,
      ).then(function (result) {
        logger.info(`--> ` + result.rows);
        return connection.close();
      }).catch(function (error) {
        logger.info(`--> ` + error.message);
        return connection.close();
      });
    }).catch(function (error) {
      throw new OracleError(
        new Error(error),
      );
    });
  }

As you can see I checked the entries, everything is fine before oracledb.getConnection after that I don't understand what is happenning

Can someone Help me with this ?

EDIT :

For information executeBlockFunction content was taken from oracleDb npm documentation :

Oracle documentation link

Upvotes: 0

Views: 1527

Answers (1)

alexmac
alexmac

Reputation: 19617

You should add await keyword in executeBlockFunction function, or remove async keyword and return result:

Solution 1:

async executeBlockFunction(block: any, bindVars: any): Promise<any> {
    await oracledb.getConnection(...)

Solution 2:

executeBlockFunction(block: any, bindVars: any): Promise<any> {
    return oracledb.getConnection(...)

Or, so as you already using async/await you can refactor/simplify your code:

async executeBlockFunction(block: any, bindVars: any): Promise<any> {
  try {
    let connection = await oracledb.getConnection({
      user: config.oracle.user,
      password: config.oracle.password,
      connectString: config.oracle.connectString,
    });
    logger.info(`--> connected`);

    let result = await connection.execute(block, bindVars);
    logger.info(`--> ` + result.rows);

    return result;
  } catch (err) {
    logger.info(`--> ` + error.message);
    throw new OracleError(err));
  } finally {
    return connection.close();
  }
}

Upvotes: 3

Related Questions