You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Hedera SDK调用getReceipt()报_logger未定义错误的原因与解决

问题:调用.getReceipt()时抛出_logger未定义错误,但交易执行成功

代码复现

执行以下代码时,交易本身成功完成,但调用getReceipt()抛出错误:

const hcsTopicCreateTx = await new TopicCreateTransaction()
    .freezeWith(client)
    .sign(operatorKey);
const hcsTopicCreateTxResponse = await hcsTopicCreateTx.execute(client);
const hcsTopicCreateReceipt = await hcsTopicCreateTxResponse.getReceipt();

客户端初始化代码:

const client = Client.forTestnet().setOperator(operatorId, operatorKey);

也曾尝试手动设置logger,但问题依旧:

const client = Client.forTestnet().setOperator(operatorId, operatorKey);
client.setLogger(new Logger(LogLevel.Warn));

错误信息

file:///Users/user/code/hedera/hedera-code-snippets/hcs-topic-permissioned-write/node_modules/@hashgraph/sdk/src/Executable.js:518
                ? client._logger != null
                         ^
TypeError: Cannot read properties of undefined (reading '_logger')
    at TransactionReceiptQuery.execute (file:///Users/user/code/hedera/hedera-code-snippets/hcs-topic-permissioned-write/node_modules/@hashgraph/sdk/src/Executable.js:518:26)
    at TransactionResponse.getReceipt (file:///Users/user/code/hedera/hedera-code-snippets/hcs-topic-permissioned-write/node_modules/@hashgraph/sdk/src/transaction/TransactionResponse.js:81:54)
    at main (file:///Users/user/code/hedera/hedera-code-snippets/hcs-topic-permissioned-write/hcs-write.js:60:66)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

问题原因

这是Hedera JavaScript SDK旧版本中的一个bug:TransactionResponse.getReceipt()方法内部发起TransactionReceiptQuery时,没有正确传入客户端实例,导致查询执行时client参数为undefined,进而触发访问_logger的错误。手动设置logger无法解决问题,因为根源是查询未获取到有效的客户端对象。

解决方案

方案1:手动构建TransactionReceiptQuery

替代默认的getReceipt()调用,手动创建查询并明确传入客户端:

const hcsTopicCreateTx = await new TopicCreateTransaction()
    .freezeWith(client)
    .sign(operatorKey);
const hcsTopicCreateTxResponse = await hcsTopicCreateTx.execute(client);
// 手动创建收据查询并指定客户端
const hcsTopicCreateReceipt = await new TransactionReceiptQuery()
    .setTransactionId(hcsTopicCreateTxResponse.transactionId)
    .execute(client);

方案2:升级Hedera SDK版本

该bug在Hedera SDK v2.17.0及后续版本中已被修复,直接升级依赖即可:

npm install @hashgraph/sdk@latest

升级后可继续使用原有的getReceipt()调用逻辑。

内容的提问来源于stack exchange,提问作者bguiz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.25 03:17:26