javascript - NodeJS : Unhandled promise rejection

标签 javascript node.js oracle promise es6-promise

我遇到了一个小问题,在调试了所有应用程序后,我注意到这是导致问题的文件,并向我返回了 UnhandledPromiseRejection

'use strict'

const connection = require('../models/'),
      oracledb   = require('oracledb'),
      conexion   = oracledb.getConnection(connection)
oracledb.outFormat = oracledb.OBJECT;

module.exports  = {
  index(req, res) {
  conexion.then(con => {
    return con.execute(
      `SELECT id_application, name, description, creation_date ` +
      `FROM application `
    ).then(bucket => {
      return con.execute(
        `SELECT id_definition, id_application, field_name_original, field_name_new,
        column_name, position, id_type_data, field_size, creation_date,
        description, filter, visible ` +
        `FROM definition `
      ).then(definitions => {
        res.status(200).json(creaJSON(bucket, definitions))
      }).catch(error  => { return res.status(500).json({'message': error}) })
    }).catch(err  =>  { return res.status(500).json({'message': err}) })
  }).catch(err  =>  { return res.status(500).json({'message': err}) })
  },
  create(req, res)  {
  },
  update(req, res)  {
  }
}

const doRelease = (connection) => {
  connection.close((err)  =>  {
    if(err) console.error(err.message);
  })
}

const creaJSON = (buckets, definitions)  => {
  var df = new Array()
  buckets['rows'].map(obj =>  {
    definitions['rows'].map(def =>  {
      if(obj['ID_APPLICATION'] == def['ID_APPLICATION']) df.push(def)
    })
    obj['Definitions'] = df
    df = []
  })
  return buckets.rows
}

UnhandledPromiseRejection之后是:错误:ORA-12170:TNS:发生连接超时

(node:1270) DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.JS process with a non-zero exit code.

我已经在寻找解决方案,有人说 promise 没有正确捕获,但我没有看到它们有任何问题。还有其他建议吗?

欢迎任何帮助。

谢谢

最佳答案

const connection = require('../models/'),
  oracledb   = require('oracledb'),
  conexion   = oracledb.getConnection(connection)

正在将 conexion 设置为执行整个源文件时调用 .getConnection 返回的 promise (响应要求)。

conexion 此时没有处理程序。仅当调用导出的 {index, create, update} 对象的 index 方法时才会添加处理程序。

因此,需要的源文件和调用的 index 之间的连接超时将产生未处理的拒绝错误。

显然添加了一个catch子句,例如

 conexion   = oracledb.getConnection(connection).catch( onRejected)

应该修复此错误,但是您想要在 onRejected 编码中投入多少恢复取决于您。

<小时/> 编辑:

满足 V8 版本处理未捕获的 Promise 拒绝的一个不太明显的方法是提供一个虚拟处理程序来阻止它:

conexion   = oracledb.getConnection(connection);
conexion.catch(()=>undefined); // a do nothing catch handler.

这里第二行向 conexion Promise 添加了一个处理程序,使其成为“已处理”,这可以防止它成为未捕获的 Promise 拒绝。 catch 返回的 Promise 是多余的,不会被记录,但如果调用无操作 catch 处理程序,就会实现

现在,在调用 index 之前,可以拒绝 conexion 中持有的 promise ,而不会生成异常。这是否是为特定应用程序编写 Promise 拓扑的最佳方式是另一个问题 - 您可能非常希望尽早解决连接超时问题。

关于javascript - NodeJS : Unhandled promise rejection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43287552/

相关文章:

javascript - 从 React 中的自定义元素修改属性

javascript - 检查 JavaScript 字符串是否为 URL

node.js - Sequelize addColumn 迁移始终引用公共(public)架构

javascript - 如何编写 Mongoose 查询来过滤子文档

oracle - 如何在Gitlab-ci中执行SQL文件

sql - oracle next_day函数使用天数

javascript - 查找函数参数的数量及其名称

javascript - 将 Canvas 生成的 Img(图像)复制到 MS-Word

Node.js 中的 JavaScript 作用域

sql - 从Oracle表中选择所有列和常量值