簡體   English   中英

NodeJS:未處理的承諾拒絕

[英]NodeJS: Unhandled promise rejection

我遇到了一個小問題,在調試了所有應用程序之后,我注意到這是導致問題的文件,並向我返回了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
}

之后跟隨UnhandledPromiseRejectionError: 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.

我已經在尋找解決方案,有人說諾言沒有正確兌現,但我認為它們沒有任何問題。 還有其他建議嗎?

任何幫助都將受到歡迎。

謝謝

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. 

在這里,第二行在共conexion承諾中添加了處理程序,使其成為“已處理”,從而防止了它成為未捕獲的承諾拒絕。 catch所返回的承諾是多余的,沒有記錄,但是如果曾經調用過無操作的catch處理程序,則將兌現

現在,可以在調用index之前拒絕共存的諾言,而不會產生異常。 這是否是為特定應用程序編寫承諾拓撲的最佳方法是另一個問題-您可能希望早日解決連接超時問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM