【发布时间】:2021-08-19 09:25:21
【问题描述】:
我通过邮递员执行 GET 请求时出现此错误,该请求是一个简单的 SELECT 查询:
(node:4804) UnhandledPromiseRejectionWarning: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
at ServerResponse.setHeader (_http_outgoing.js:558:11)
at ServerResponse.header (C:\Users\lurrechaga\source\repos\Infraestructuras\InfraestructurasAPI\node_modules\express\lib\response.js:771:10)
at ServerResponse.send (C:\Users\lurrechaga\source\repos\Infraestructuras\InfraestructurasAPI\node_modules\express\lib\response.js:170:12)
at ServerResponse.json (C:\Users\lurrechaga\source\repos\Infraestructuras\InfraestructurasAPI\node_modules\express\lib\response.js:267:15)
at C:\Users\lurrechaga\source\repos\Infraestructuras\InfraestructurasAPI\SensoresControll\api.js:40:18
at processTicksAndRejections (internal/process/task_queues.js:93:5)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:4804) 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(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:4804) [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.
我一直在寻找这个错误,发现人们错过了返回,抛出了多个异步函数或错过了 try catch,我找不到适合我的解决方案。
我在本地机器上使用 express、body-parser 和 SQL server Express
这是发出请求时使用的代码:
请求:
http://localhost:8090/api/grafico/datos/temperatura/medicionAire/2059E7
API.js:
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(cors());
app.use('/api', router);
router.use((request, response, next) => {
console.log('middleware');
console.log(next());
next();
});
router.route('/grafico/datos/:medicion/:tipoMedicion/:id').get((request, response) => {
console.log("graficodato");
Db.getMedicionGrafico(request.params.medicion, request.params.tipoMedicion, request.params.id).then((data) => {
response.json(data);
})})
FunctionAPI.js:
var config = require('./dbconfig');
const sql = require('mssql');
.
.
.
async function getMedicionGrafico(medicion, tipoMedicion, id) {
try {
console.log("agetmedG");
let pool = await sql.connect(config);
let medicionSensor = await pool.request()
.input('input_medicion', sql.VarChar, medicion)
.input('input_tipoMedicion', sql.VarChar, tipoMedicion)
.input('input_id', sql.VarChar, id)
.query("SELECT TOP 100 " + medicion + " FROM " + tipoMedicion + " WHERE @input_id = id ORDER BY fecha DESC;");
return medicionSensor.recordsets;
}
catch (error) {
console.log(error);
}}
配置:
const config = {
user: 'RealUSer', // sql user
password: 'RealPasswordForsure', //sql user password
server: 'localhost',
database: 'SensoresAire',
options: {
trustedconnection: true,
enableArithAbort: true,
instancename: 'SQLEXPRESS' // SQL Server instance name
},
port: 1433
}
module.exports = config;
【问题讨论】:
标签: javascript express body-parser