gpt4 book ai didi

javascript - 为什么我收到 UnhandledPromiseRejectionWarning : Unhandled promise rejection?

转载 作者:行者123 更新时间:2023-11-29 09:46:44 26 4
gpt4 key购买 nike

脚本一直运行,直到它尝试关闭数据库连接,现在它给了我一个 promise 错误。

当我注释掉此部分时,脚本运行良好,没有错误,但是当然,数据库连接永远不会关闭...

>     // .then(() => {
> // database.close();
> // }, err => {
> // return database.close().then(() => {throw err; });
> // })

在 Promise 链的末尾有一个 catch block 还不够吗?

我收到以下错误:

[ERROR] (node:9376) 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(). (rejection id: 1) [[09:33:03.101]] [ERROR] (node:9376) [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.

mysql.js

const mysql = require("mysql");

const mySQLConnectionDetails = {
host: "localhost",
user: "xxxx",
password: "xxxx",
database: "amdm",
multipleStatements: true
};

class Database {
constructor(config) {
this.connection = mysql.createConnection(config);
}
query(sql, args) {
return new Promise((resolve, reject) => {
this.connection.query(sql, args, (err, rows) => {
if (err)
return reject(err);
resolve(rows);
});
});
}
close() {
return new Promise((resolve, reject) => {
this.connection.end(err => {
if (err)
return reject(err);
resolve();
});
});
}
}
module.exports = { Database, mySQLConnectionDetails };

index.js

require("console-stamp")(console, "[HH:MM:ss.l]");
const express = require("express");
const router = express.Router();
const { Database, mySQLConnectionDetails} = require("../helpers/mySQL");
const { cleanseString, cleanseStringToArray, generateCountDuplication, sortValuesBykey, generateScoreArray, calculateAverage, replaceAbbreviationWithExpansion } = require("../helpers/stringFixers");

const database = new Database(mySQLConnectionDetails);


/* GET Clean original name of part and store it in database. */
router.get("/createcleanname", (req, res) => {
let allParts = [];
let eClassCodes = []; // here I define eClassCodes
let abbreviationsAndExpansions = [];


database.query("SELECT * FROM partnumbersclassified")
.then(rows => {
console.log(rows.length);
allParts = rows;
return database.query("SELECT * FROM amdm.abbreviationexpansion");
})
.then(rows => {
abbreviationsAndExpansions = rows;
return database.query("SELECT * FROM eclasstree WHERE numberofdigits = '8'");
})
.then(rows => {
eClassCodes = rows; // Here I assign the values to the variable.
const replaceAbbreviations = replaceAbbreviationWithExpansion(allParts, abbreviationsAndExpansions);
console.log("replaceAbbreviations finished");
const cleanseStrings = cleanseString(replaceAbbreviations, "cleanname");
console.log("cleanseStrings finished");
return cleanseStrings;
})
.then(result => {
result.forEach(obj => {
return database.query(`UPDATE partnumbersclassified SET cleanname = '${obj.cleanname}' WHERE itemnumber = '${obj.itemnumber}'`);
});
console.log("cleanname updated on all parts");
})
.then(() => {
database.close();
}, err => {
return database.close().then(() => {throw err; });
})
.catch(err => {throw err;});
});

最佳答案

而不是在 catch 中抛出错误,执行其预期目的:处理错误。

就您的情况而言,您可能只想记录错误。

.then(() => {
database.close();
}, err => {
return database.close().then(() => {throw err; });
})
.catch(err => {
console.log(err)
})

在生产环境中,您可能需要使用错误跟踪器,例如 Sentry记录并处理错误。

关于javascript - 为什么我收到 UnhandledPromiseRejectionWarning : Unhandled promise rejection?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55531077/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com