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

Hedera本地网络报错:Cannot read properties of undefined (reading '0')

搭建Hedera本地网络时出现TypeError: Cannot read properties of undefined (reading '0')的修复方案

降级docker-compose后运行Hedera本地网络代码时,触发TypeError: Cannot read properties of undefined (reading '0')错误,期望输出新创建的账户ID,后续还出现其他新错误,相关代码如下:

const {
    Client,
    PrivateKey,
    Hbar,
    AccountId,
    AccountCreateTransaction,
} = require("@hashgraph/sdk");

async function main() {
    //Create your local client
    const node = {"127.0.0.1:50211": new AccountId(3)}
    const client = Client.forNetwork(node).setMirrorNetwork("127.0.0.1:5600");
    //Set the transaction fee paying account
    client.setOperator(AccountId.fromString("0.0.2"),PrivateKey.fromString("302e020100300506032b65700..."));
    //Submit a transaction to your local node
    const newAccount = await new AccountCreateTransaction()
        .setKey(PrivateKey.fromString("302e020100300506032b65700..."))
        .setInitialBalance(new Hbar(316))
        .execute(client);
    //Get receipt
    const receipt = await newAccount.getReceipt(client);
    //Get the account ID
    const newAccountId = receipt.accountId;
    console.log(newAccountId);
}
void main();

修复方案

  • 修正镜像网络配置格式
    setMirrorNetwork要求传入数组格式的地址,原代码传入字符串会导致SDK尝试读取数组索引时失败。修改为:

    const client = Client.forNetwork(node).setMirrorNetwork(["127.0.0.1:5600"]);
    

    这是触发reading '0'错误的直接原因。

  • 确保私钥完整有效
    代码中私钥被截断(302e020100300506032b65700...),需替换为本地网络账户0.0.2的完整私钥(本地网络默认私钥通常为302e020100300506032b657004200123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef,可根据docker-compose配置文件确认)。

  • 检查本地容器运行状态
    降级docker-compose后,需确认所有Hedera相关容器正常启动:

    docker ps
    

    若容器未正常运行,重启容器:

    docker-compose down
    docker-compose up -d
    
  • 兼容SDK与网络版本
    降级docker-compose后,本地Hedera网络版本可能降低,需确保@hashgraph/sdk版本与网络版本匹配。可查看Hedera官方文档确认对应版本,然后调整依赖:

    npm install @hashgraph/sdk@<对应版本号>
    
  • 排查后续错误
    若修复后仍有报错,需:

    • 确认节点grpc端口50211与docker-compose中配置的节点端口一致
    • 检查交易提交后节点是否返回有效响应,可通过增加日志打印排查:
      console.log("Client configuration:", client);
      

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.28 19:17:39