Estoy intentando recibir una consulta SQL
utilizando NodeJs
pero me arroja los siguientes errores:
(node:1876336) UnhandledPromiseRejectionWarning: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
(node:1876336) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch().
(node:1876336) [DEP0018] 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.
El código server.js
:
const express = require('express')
const bodyParser = require('body-parser')
const router = express.Router()
const oracledb = require('oracledb')
const cors = require('cors')
const app = express()
app.use(cors())
app.use(bodyParser.urlencoded({extended: true}))
const connAttrs = {
user: 'bi_src',
password: 'bi_src',
connectString:
'(DESCRIPTION =(LOAD_BALANCE = ON)(FAILOVER = ON)(ADDRESS =(PROTOCOL = TCP)(HOST = 192.168.112.95)(PORT = 1521))(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.112.95)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=orcl)(FAILOVER_MODE=(TYPE=SELECT)(METHOD = BASIC))))',
}
app.get('/ejemplo', function (req, res) {
oracledb.getConnection(connAttrs, function (err, connection) {
if (err) {
// Error connecting to DB
res.set('Content-Type', 'application/json')
res.status(500).send(
JSON.stringify({
status: 500,
message: 'Error connecting to DB',
detailed_message: err.message,
})
)
return
}
connection.execute(
'SELECT G300CTA FROM BI_SRC.GSCF300',
{},
{
outFormat: oracledb.OBJECT, // Return the result as Object
},
function (err, result) {
if (err) {
res.set('Content-Type', 'application/json')
res.status(500).send(
JSON.stringify({
status: 500,
message:
'Error getting the dba_tablespaces',
detailed_message: err.message,
})
)
} else {
res.status(202).send({
message: 'Resultados',
result })
}
// Release the connection
connection.release(function (err) {
if (err) {
console.error(err.message)
} else {
res.status(202).send({
message: 'Petición exitosa',
})
}
})
}
)
})
})
app.listen(4500, () => {
console.log('Live at port 4500');
})
A veces me devuelve la consulta y otras veces me sale el error. No entiendo la razón.