From 5ee6caa190e86864f0416a6a55aa51f5dd5a4de0 Mon Sep 17 00:00:00 2001 From: hailin Date: Sun, 11 Jan 2026 09:57:21 -0800 Subject: [PATCH] =?UTF-8?q?fix(contribution-service):=20=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?Kafka=E6=B6=88=E6=81=AFBigInt=E5=BA=8F=E5=88=97=E5=8C=96?= =?UTF-8?q?=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit JSON.stringify无法序列化BigInt,添加自定义replacer将BigInt转换为字符串 Co-Authored-By: Claude Opus 4.5 --- .../infrastructure/kafka/kafka-producer.service.ts | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/backend/services/contribution-service/src/infrastructure/kafka/kafka-producer.service.ts b/backend/services/contribution-service/src/infrastructure/kafka/kafka-producer.service.ts index 6227552c..28f83f9e 100644 --- a/backend/services/contribution-service/src/infrastructure/kafka/kafka-producer.service.ts +++ b/backend/services/contribution-service/src/infrastructure/kafka/kafka-producer.service.ts @@ -25,7 +25,7 @@ export class KafkaProducerService implements OnModuleInit { await lastValueFrom( this.kafkaClient.emit(topic, { key: message.key, - value: JSON.stringify(message.value), + value: JSON.stringify(message.value, this.bigIntReplacer), headers: message.headers, }), ); @@ -36,6 +36,16 @@ export class KafkaProducerService implements OnModuleInit { } } + /** + * JSON.stringify replacer 函数,将 BigInt 转换为字符串 + */ + private bigIntReplacer(_key: string, value: any): any { + if (typeof value === 'bigint') { + return value.toString(); + } + return value; + } + async emitBatch(topic: string, messages: KafkaMessage[]): Promise { try { for (const message of messages) {