简体   繁体   English

如何在节点 js 中记录特定的错误消息?

[英]how to log specific error message in node js?

I am working with sql in node js Now if some error occurs executing SQL query due to the wrong query then I get an error message along with the data in the query.我正在节点 js 中使用 sql 现在,如果由于错误的查询而在执行 SQL 查询时发生一些错误,那么我会收到一条错误消息以及查询中的数据。 So is there any way where only the error message should be printed not the data along.那么有什么方法可以只打印错误消息而不是打印数据。 for eg.例如。 If the table name is incorrect in SQL query then I get an error message as an incorrect table name but the query with data also gets printed as part of the error message.如果 SQL 查询中的表名不正确,那么我会收到一条错误消息作为an incorrect table name但带有数据的查询也会作为错误消息的一部分打印出来。

Please tell me how I can avoid it?请告诉我如何避免它? Below is sql query error along with query i want to avoid printing query data下面是 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

Looks like the error that is being displayed contains some properties like code , errno , sqlMessage etc.看起来正在显示的错误包含一些属性,如codeerrnosqlMessage等。

Maybe you can just console.error(yourError.sqlMessage)也许你可以只使用console.error(yourError.sqlMessage)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM