Nest.js 问题, 1:树形数据存修改如何操作。2: typeorm 异常如何处理

254 天前
 LandCruiser
{
        "create_time": "2023-08-21T08:27:50.146Z",
        "update_time": "2023-08-24T07:54:34.000Z",
        "id": 1,
        "name": "部门 1",
        "code": "1111",
        "down_dep": [
            {
                "create_time": "2023-08-21T08:27:50.146Z",
                "update_time": "2023-08-24T07:55:25.000Z",
                "id": 22,
                "name": "部门 2",
                "code": "100"
            }
        ],
        "up_dep": null
    },
    

这种接口的数据怎么修改? 还有一个问题就是 DTO 中没有 create_time ,update_time 两个字段,修改时传入就会报错。

   at Query.onResult (C:\pm-server\src\driver\mysql\MysqlQueryRunner.ts:222:33)
   at Query.execute (C:\pm-server\node_modules\mysql2\lib\commands\command.js:36:14)
   at PoolConnection.handlePacket (C:\pm-server\node_modules\mysql2\lib\connection.js:478:34)
   at PacketParser.onPacket (C:\pm-server\node_modules\mysql2\lib\connection.js:97:12)
   at PacketParser.executeStart (C:\pm-server\node_modules\mysql2\lib\packet_parser.js:75:16)
   at Socket.<anonymous> (C:\pm-server\node_modules\mysql2\lib\connection.js:104:25)
   at Socket.emit (node:events:513:28)
   at addChunk (node:internal/streams/readable:324:12)
   at readableAddChunk (node:internal/streams/readable:297:9)
   at Socket.Readable.push (node:internal/streams/readable:234:10) {
 query: 'INSERT INTO `dept`(`create_time`, `update_time`, `id`, `name`, `code`, `upDepId`) VALUES (DEFAULT, DEFAULT, DEFAULT, ?, ?, DEFAULT)',
 parameters: [ '456456', '1111' ],
 driverError: Error: Duplicate entry '456456' for key 'IDX_5eb4a4c9f25934f105299edffd'
     at Packet.asError (C:\pm-server\node_modules\mysql2\lib\packets\packet.js:728:17)
     at Query.execute (C:\pm-server\node_modules\mysql2\lib\commands\command.js:29:26)
     at PoolConnection.handlePacket (C:\pm-server\node_modules\mysql2\lib\connection.js:478:34)
     at PacketParser.onPacket (C:\pm-server\node_modules\mysql2\lib\connection.js:97:12)
     at PacketParser.executeStart (C:\pm-server\node_modules\mysql2\lib\packet_parser.js:75:16)
     at Socket.<anonymous> (C:\pm-server\node_modules\mysql2\lib\connection.js:104:25)
     at Socket.emit (node:events:513:28)
     at addChunk (node:internal/streams/readable:324:12)
     at readableAddChunk (node:internal/streams/readable:297:9)
     at Socket.Readable.push (node:internal/streams/readable:234:10) {
   code: 'ER_DUP_ENTRY',
   errno: 1062,
   sqlState: '23000',
   sqlMessage: "Duplicate entry '456456' for key 'IDX_5eb4a4c9f25934f105299edffd'",
   sql: "INSERT INTO `dept`(`create_time`, `update_time`, `id`, `name`, `code`, `upDepId`) VALUES (DEFAULT, DEFAULT, DEFAULT, '456456', '1111', DEFAULT)"
 },
 code: 'ER_DUP_ENTRY',
 errno: 1062,
 sqlState: '23000',
 sqlMessage: "Duplicate entry '456456' for key 'IDX_5eb4a4c9f25934f105299edffd'",
 sql: "INSERT INTO `dept`(`create_time`, `update_time`, `id`, `name`, `code`, `upDepId`) VALUES (DEFAULT, DEFAULT, DEFAULT, '456456', '1111', DEFAULT)"
}```


值重复异常如何处理?自己写正则吗?
1202 次点击
所在节点    JavaScript
9 条回复
musi
254 天前
俩问题都看不懂
1. 修改数据不是改数据库的吗,直接用 typeorm 提供的 API 不就可以改了?
2. 值重复不是数据库的限制吗?你到底想不想要这个限制,不想要去掉就好了
LandCruiser
254 天前
@musi 不行啊,update 不了,问题其实。第二个问题,我肯定是要这个字段的 unique 限制的,但是数据库返回给我的信息是一段 英文 Duplicate entry '456456' for key 'IDX_5eb4a4c9f25934f105299edffd'。 我现在想返回给用户的是{code:400 ,messgae:‘部门名称重复’}。这个过程需要我写什么代码呢? intercepetor ?还是什么
guiling
254 天前
第一个问题我估计你配了关联关系吧,一般更新最好还是单表更新,这种级联的不太好控制,create_time ,update_time 可以考虑分别配置成 CURRENT_TIMESTAMP ,CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP ,数据库会自动为你更新

Duplicate entry xx for key 就是唯一索引重复,出数字段可以考虑直接用自增,一定要自己控制的话就封装个唯一 id 生成的工具类
guiling
254 天前
如果是要捕获异常的话,唯一键的只能匹配关键词了,但一般出现这种情况就说明系统有问题了,因该考虑修复
lzgshsj
254 天前
异常就用 exception filter 啊,@Catch('xxx'),xxx 就是 typeorm 的错误类
juzisang
253 天前
DTO 校验一遍基本的类型错误,自己在写代码校验一遍重复值,手动抛错出去,exception filter 一般只是为了控制一些全局错误...
juzisang
253 天前
别想着框架或者库帮你干这些,顶多帮你校验一些基本的错误,和业务相关的校验还是需要自己搞手动抛的...
encro
253 天前
异常最简单的办法就是捕获,然后查询 message 包含 Duplicate entry ,就修改异常信息。
LandCruiser
252 天前
@juzisang 先查表,查有没有重复的,您是这个意思吗?先查再存,这种做法适用于生产吗?我服务端初学

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/968786

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX