使用promises请求使用nodejs进行oracle

时间:2017-09-27 16:24:46

标签: node.js oracle typescript npm promise

我试图在nodejs中的oracle中执行一个块函数,但是在10秒后我没有得到响应:

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

与此同时,代码继续执行。这是我的工作流程:

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

然后在userLogin

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

最后是功能:

  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),
      );
    });
  }

正如你所看到我检查了条目,在oracledb.getConnection之后一切都很好,之后我就不明白发生了什么

有人可以帮助我吗?

编辑:

有关信息executeBlockFunction内容取自oracleDb npm文档:

  

nodemailer

1 个答案:

答案 0 :(得分:2)

您应在await函数中添加executeBlockFunction关键字,或删除async关键字并返回结果:

解决方案1:

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

解决方案2:

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

或者,因为您已经使用async/await,您可以重构/简化您的代码:

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();
  }
}