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);
- 确认节点grpc端口
内容的提问来源于stack exchange,提问作者YJ C
相关产品推荐
相关产品推荐

