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:
parent
a40e314c94
commit
5ee6caa190
|
|
@ -25,7 +25,7 @@ export class KafkaProducerService implements OnModuleInit {
|
||||||
await lastValueFrom(
|
await lastValueFrom(
|
||||||
this.kafkaClient.emit(topic, {
|
this.kafkaClient.emit(topic, {
|
||||||
key: message.key,
|
key: message.key,
|
||||||
value: JSON.stringify(message.value),
|
value: JSON.stringify(message.value, this.bigIntReplacer),
|
||||||
headers: message.headers,
|
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> {
|
async emitBatch(topic: string, messages: KafkaMessage[]): Promise<void> {
|
||||||
try {
|
try {
|
||||||
for (const message of messages) {
|
for (const message of messages) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue