2017-04-07 151 views
0

我有一个小问题,调试所有的应用程序后,我注意到,这是造成问题的文件,返回我UnhandledPromiseRejection的NodeJS:未处理的承诺拒绝

'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后,被跟踪由:Error: ORA-12170: TNS:Connect timeout occurred

(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. 

我已经找了解决方案,有的说应许不要再追正确,但我看不出有任何问题与他们。任何其他建议?

任何帮助将受到欢迎。

由于

回答

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

是设置conexion到通过调用.getConnection当执行整个源文件(响应于被要求)作出返回的承诺。

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

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

显然增加一个catch子句如

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

应该可以解决这个错误,但多少恢复要投入编码onRejected是你。


编辑:

一个不太明显的方式来满足V8的版本如何处理未捕获承诺拒绝是提供一个虚拟的处理程序,以阻止它:

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

下面第二行处理程序添加到承诺,使其“处理”,从而防止它成为未被捕获的承诺拒绝。由catch返回的许诺是多余的并且未被记录,但是如果没有操作无操作捕获处理程序,将会执行

现在承诺conexion可以被拒绝之前调用index而不会产生异常。这是否是为特定应用程序编码承诺拓扑的最佳方式,这是另一个问题 - 您可能很希望尽早解决连接超时问题。

+0

不错,我现在不能尝试,因为我不在工作,但星期一会检查它。非常感谢。 @ Traktor53 –

+0

很好,谢谢,'''UnhandledPromiseRejection'''不见了,现在我只有'''错误:ORA-12170:TNS:发生连接超时'''哈哈哈哈。我会检查一下,如果我能解决这个问题,如果没有,我会打开另一个问题。谢谢你,先生。 –

相关问题