gpt4 book ai didi

javascript - 如何在 Node js中记录特定的错误消息?

转载 作者:行者123 更新时间:2023-12-03 07:56:22 25 4
gpt4 key购买 nike

我正在节点js中使用sql现在,如果由于错误的查询而在执行SQL查询时发生一些错误,那么我会收到一条错误消息以及查询中的数据。因此,有什么方法可以只打印错误消息而不打印数据。
例如如果表名称在SQL查询中不正确,那么我会收到一条错误消息,即an incorrect table name,但带有数据的查询也会作为错误消息的一部分被打印出来。
请告诉我如何避免呢?下面是SQL查询错误以及查询我想避免打印查询数据

[Logs] :- Error: ER_NO_SUCH_TABLE: Table 'service.journal_tabe' doesn't exist
at Query.Sequence._packetToError (/home/ubuntu/Documents/my_project-V2/my_project-data-service/node_modules/mysql/lib/protocol/sequences/Sequence.js:47:14)
at Query.ErrorPacket (/home/ubuntu/Documents/my_project-V2/my_project-data-service/node_modules/mysql/lib/protocol/sequences/Query.js:79:18)
at Protocol._parsePacket (/home/ubuntu/Documents/my_project-V2/my_project-data-service/node_modules/mysql/lib/protocol/Protocol.js:291:23)
at Parser._parsePacket (/home/ubuntu/Documents/my_project-V2/my_project-data-service/node_modules/mysql/lib/protocol/Parser.js:433:10)
at Parser.write (/home/ubuntu/Documents/my_project-V2/my_project-data-service/node_modules/mysql/lib/protocol/Parser.js:43:10)
at Protocol.write (/home/ubuntu/Documents/my_project-V2/my_project-data-service/node_modules/mysql/lib/protocol/Protocol.js:38:16)
at Socket.<anonymous> (/home/ubuntu/Documents/my_project-V2/my_project-data-service/node_modules/mysql/lib/Connection.js:88:28)
at Socket.<anonymous> (/home/ubuntu/Documents/my_project-V2/my_project-data-service/node_modules/mysql/lib/Connection.js:526:10)
at Socket.emit (events.js:311:20)
at Socket.EventEmitter.emit (domain.js:482:12)
at addChunk (_stream_readable.js:294:12)
at readableAddChunk (_stream_readable.js:275:11)
at Socket.Readable.push (_stream_readable.js:209:10)
at TCP.onStreamRead (internal/stream_base_commons.js:186:23)
--------------------
at Protocol._enqueue (/home/ubuntu/Documents/my_project-V2/my_project-data-service/node_modules/mysql/lib/protocol/Protocol.js:144:48)
at Connection.query (/home/ubuntu/Documents/my_project-V2/my_project-data-service/node_modules/mysql/lib/Connection.js:198:25)
at /home/ubuntu/Documents/my_project-V2/my_project-data-service/src/services/jounal.service.ts:48:17
at new Promise (<anonymous>)
at JournalService.<anonymous> (/home/ubuntu/Documents/my_project-V2/my_project-data-service/src/services/jounal.service.ts:47:16)
at Generator.next (<anonymous>)
at fulfilled (/home/ubuntu/Documents/my_project-V2/my_project-data-service/node_modules/tslib/tslib.js:110:62)
at processTicksAndRejections (internal/process/task_queues.js:97:5) {
code: 'ER_NO_SUCH_TABLE',
errno: 1146,
sqlMessage: "Table 'service.journal_tabe' doesn't exist",
sqlState: '42S02',
index: 0,
sql: "INSERT INTO journal_tabe (id, businessId, journalDate, transactionId, transactionType, number, contactId,description,accountId, amount,isReconciled, active, version) VALUES ('0d47a468-610c-4011-9012-ed9f8e6dcf07', 'b6feb1af-31ff-4b08-9545-cd5b5747e0ba', '2020-07-08 00:00:00', '98', 'Journal Entry', '4', '1', '', '131', 991, false, true, 1), ('29489e63-6b1d-49fc-b7e0-6a06696997b0', 'b6feb1af-31ff-4b08-9545-cd5b5747e0ba', '2020-07-08 00:00:00', '98', 'Journal Entry', '', '1', '', '131', -991, false, true, 1), ('04af0569-c685-45e0-bbc1-35edaa55ca3c', 'b6feb1af-31ff-4b08-9545-cd5b5747e0ba', '2020-07-08 00:00:00', '99', 'Journal Entry', '5', '2', '', '131', 505, false, true, 1), ('9d5dc24f-b2ec-4dd2-9de1-112ec2115ae4', 'b6feb1af-31ff-4b08-9545-cd5b5747e0ba', '2020-07-08 00:00:00', '99', 'Journal Entry', '', '2', '', '131', -505, false, true, 1), ('e4ab350c-3993-41d9-b911-946282c3ab96', 'b6feb1af-31ff-4b08-9545-cd5b5747e0ba', '2020-08-13 00:00:00', '96', 'Journal Entry', '2', '0', '', '131', 100, false, true, 1), ('2e9c7e16-592a-40ca-ae2f-ec2eb2b3e323', 'b6feb1af-31ff-4b08-9545-cd5b5747e0ba', '2020-08-13 00:00:00', '96', 'Journal Entry', '', '0', '', '131', -100, false, true, 1), ('aa1849d2-93b5-4066-b952-774f2521ceb6', 'b6feb1af-31ff-4b08-9545-cd5b5747e0ba', '2020-08-13 00:00:00', '97', 'Journal Entry', '3', '1', '', '131', 201, false, true, 1), ('c1b84ead-cfcd-4dcd-88fe-174188fad7ab', 'b6feb1af-31ff-4b08-9545-cd5b5747e0ba', '2020-08-13 00:00:00', '97', 'Journal Entry', '', '1', '', '131', -201, false, true, 1), ('6a757d6e-7ab2-48e

最佳答案

看起来正在显示的错误包含一些属性,例如代码 errno sqlMessage 等。
也许你可以console.error(yourError.sqlMessage)

关于javascript - 如何在 Node js中记录特定的错误消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63668961/

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