fix(contribution-service): 修复Kafka消息BigInt序列化错误

JSON.stringify无法序列化BigInt,添加自定义replacer将BigInt转换为字符串

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
hailin 2026-01-11 09:57:21 -08:00
parent a40e314c94
commit 5ee6caa190
1 changed files with 11 additions and 1 deletions

View File

@ -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<void> {
try {
for (const message of messages) {