[英]Javascript Uncaught TypeError: Cannot read property 'length' of undefined (using an array)
[英]TypeError: Cannot read property 'length' of undefined - While deploying using hardhat
尝试在安全帽开发链上部署模拟合约时出现以下错误。
我正在关注:
通过 JavaScript 学习区块链、Solidity 和全栈 Web3 开发 – Patrick Collins 在 FreeCodeCamp YT 频道上的 32 小时课程
我在上述课程的 10:48:53 时收到此错误。 我几乎尝试了所有事情,但没有任何帮助,但我找到了问题所在。
问:我认为问题出在哪里? (12:26)
据我说,问题出在我们试图部署脚本的那一行,我们正在等待返回 promise。
这是错误:
Error: ERROR processing /Users/rishavsharma/Documents/working/Web Dev/Blockchain/Freecodecamp/solidity&Block/blockchain-with-js/buyMeACoffee-dapp/deploy/00-deploy-mocks.js:
TypeError: Cannot read property 'length' of undefined
at getFrom (/Users/rishavsharma/Documents/working/Web Dev/Blockchain/Freecodecamp/solidity&Block/blockchain-with-js/buyMeACoffee-dapp/node_modules/hardhat-deploy/src/helpers.ts:1713:14)
at _deploy (/Users/rishavsharma/Documents/working/Web Dev/Blockchain/Freecodecamp/solidity&Block/blockchain-with-js/buyMeACoffee-dapp/node_modules/hardhat-deploy/src/helpers.ts:533:9)
at processTicksAndRejections (internal/process/task_queues.js:95:5)
at _deployOne (/Users/rishavsharma/Documents/working/Web Dev/Blockchain/Freecodecamp/solidity&Block/blockchain-with-js/buyMeACoffee-dapp/node_modules/hardhat-deploy/src/helpers.ts:1004:16)
at Object.module.exports [as func] (/Users/rishavsharma/Documents/working/Web Dev/Blockchain/Freecodecamp/solidity&Block/blockchain-with-js/buyMeACoffee-dapp/deploy/00-deploy-mocks.js:12:26)
at DeploymentsManager.executeDeployScripts (/Users/rishavsharma/Documents/working/Web Dev/Blockchain/Freecodecamp/solidity&Block/blockchain-with-js/buyMeACoffee-dapp/node_modules/hardhat-deploy/src/DeploymentsManager.ts:1219:22)
at DeploymentsManager.runDeploy (/Users/rishavsharma/Documents/working/Web Dev/Blockchain/Freecodecamp/solidity&Block/blockchain-with-js/buyMeACoffee-dapp/node_modules/hardhat-deploy/src/DeploymentsManager.ts:1052:5)
at SimpleTaskDefinition.action (/Users/rishavsharma/Documents/working/Web Dev/Blockchain/Freecodecamp/solidity&Block/blockchain-with-js/buyMeACoffee-dapp/node_modules/hardhat-deploy/src/index.ts:438:5)
at Environment._runTaskDefinition (/Users/rishavsharma/Documents/working/Web Dev/Blockchain/Freecodecamp/solidity&Block/blockchain-with-js/buyMeACoffee-dapp/node_modules/hardhat/src/internal/core/runtime-environment.ts:219:14)
at Environment.run (/Users/rishavsharma/Documents/working/Web Dev/Blockchain/Freecodecamp/solidity&Block/blockchain-with-js/buyMeACoffee-dapp/node_modules/hardhat/src/internal/core/runtime-environment.ts:131:14)
at DeploymentsManager.executeDeployScripts (/Users/rishavsharma/Documents/working/Web Dev/Blockchain/Freecodecamp/solidity&Block/blockchain-with-js/buyMeACoffee-dapp/node_modules/hardhat-deploy/src/DeploymentsManager.ts:1222:19)
at processTicksAndRejections (internal/process/task_queues.js:95:5)
at DeploymentsManager.runDeploy (/Users/rishavsharma/Documents/working/Web Dev/Blockchain/Freecodecamp/solidity&Block/blockchain-with-js/buyMeACoffee-dapp/node_modules/hardhat-deploy/src/DeploymentsManager.ts:1052:5)
at SimpleTaskDefinition.action (/Users/rishavsharma/Documents/working/Web Dev/Blockchain/Freecodecamp/solidity&Block/blockchain-with-js/buyMeACoffee-dapp/node_modules/hardhat-deploy/src/index.ts:438:5)
at Environment._runTaskDefinition (/Users/rishavsharma/Documents/working/Web Dev/Blockchain/Freecodecamp/solidity&Block/blockchain-with-js/buyMeACoffee-dapp/node_modules/hardhat/src/internal/core/runtime-environment.ts:219:14)
at Environment.run (/Users/rishavsharma/Documents/working/Web Dev/Blockchain/Freecodecamp/solidity&Block/blockchain-with-js/buyMeACoffee-dapp/node_modules/hardhat/src/internal/core/runtime-environment.ts:131:14)
at SimpleTaskDefinition.action (/Users/rishavsharma/Documents/working/Web Dev/Blockchain/Freecodecamp/solidity&Block/blockchain-with-js/buyMeACoffee-dapp/node_modules/hardhat-deploy/src/index.ts:584:32)
at Environment._runTaskDefinition (/Users/rishavsharma/Documents/working/Web Dev/Blockchain/Freecodecamp/solidity&Block/blockchain-with-js/buyMeACoffee-dapp/node_modules/hardhat/src/internal/core/runtime-environment.ts:219:14)
at Environment.run (/Users/rishavsharma/Documents/working/Web Dev/Blockchain/Freecodecamp/solidity&Block/blockchain-with-js/buyMeACoffee-dapp/node_modules/hardhat/src/internal/core/runtime-environment.ts:131:14)
at SimpleTaskDefinition.action (/Users/rishavsharma/Documents/working/Web Dev/Blockchain/Freecodecamp/solidity&Block/blockchain-with-js/buyMeACoffee-dapp/node_modules/hardhat-deploy/src/index.ts:669:5)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
rishavsharma@Rishavs-Air buyMeACoffee-dapp %
这是我收到错误的代码片段:
const { network } = require("hardhat");
const { developmentChains, DECIMALS, INITIAL_ANSWER } = require("../helper-hardhat-config");
module.exports = async ({ getNamedAccounts, deployments }) => {
const { deploy, log } = deployments;
const { deployer } = await getNamedAccounts();
const chainId = network.config.chainId;
if (chainId == 31337) {
console.log("Development network detected! Deploying mocks...");
const feedMock = await deploy("MockV3Aggregator", {
contract: "MockV3Aggregator",
from: deployer,
log: true,
args: [DECIMALS, INITIAL_ANSWER],
});
log("Price Feed Mock deployed!");
log("==============================================");
}
}
module.exports.tags = ["all", "mocks"];
请帮我解决这个问题我在 Ethereum Stack Exchange 上发布了这个问题,但没有得到任何帮助。
系统配置:MacBook Air M1 2021 - Monterey 12.4,yarn:1.22.18,Hardhat:2.9.6
我从 MASTER 本人那里得到了答案,@PatrickAlphaC。
当我们在上面的代码中调用 await getNamedAccounts 时,hardhat 在我们的 hardhat.config.js 中查找命名账户的长度
我们必须确保下面给出的属性在那里!
namedAccounts: {
deployer: {
default: 0, // here this will by default take the first account as deployer
},
},
这解决了我的问题,我又开心了!
确保在getNamedAccount () 中有 await。 名称也必须正确。
命名帐户必须具有正确的条目,如下所示:
namedAccounts:{部署者:{默认值:0,部署者1:0,},},
我也被这个问题困住了一段时间,并且也在遵循同样的过程。
Cannot read properties of undefined (reading 'length')
at getFrom (/Users/ilima/hh-fcc/hardhat-fundme-fcc/node_modules/hardhat-deploy/src/helpers.ts:1713:14)
at _deploy (/Users/ilima/hh-fcc/hardhat-fundme-fcc/node_modules/hardhat-deploy/src/helpers.ts:533:9)
最初我是从头开始写的,一直收到这个错误,加上安全帽 npm 安装。 我决定测试以防万一课程代码,所以我克隆了存储库,令人惊讶的是,我还在为安全帽和华夫饼的 npm 安装问题而苦苦挣扎。
npm ERR! code ERESOLVE
npm ERR! ERESOLVE unable to resolve dependency tree
npm ERR!
npm ERR! While resolving: hardhat-project@undefined
npm ERR! Found: @nomiclabs/hardhat-ethers@0.3.0-beta.13
npm ERR! node_modules/@nomiclabs/hardhat-ethers
npm ERR! dev @nomiclabs/hardhat-ethers@"npm:hardhat-deploy- ethers@^0.3.0-beta.13" from the root project
npm ERR!
npm ERR! Could not resolve dependency:
npm ERR! peer @nomiclabs/hardhat-ethers@"^2.0.0" from @nomiclabs/hardhat-waffle@2.0.2
npm ERR! node_modules/@nomiclabs/hardhat-waffle
npm ERR! dev @nomiclabs/hardhat-waffle@"^2.0.2" from the root project
npm ERR!
npm ERR! Fix the upstream dependency conflict, or retry
npm ERR! this command with --force, or --legacy-peer-deps
npm ERR! to accept an incorrect (and potentially broken) dependency resolution.
所以通过覆盖它来修复依赖hardhat-ethers,为我解决了所有问题,所以试试这个:
"devDependencies": {
...
"@nomiclabs/hardhat-ethers": "npm:hardhat-deploy-ethers@^0.3.0-beta.13",
...
},
"overrides": {
"@nomiclabs/hardhat-waffle": {
"@nomiclabs/hardhat-ethers": "$@nomiclabs/hardhat-ethers"
}
}
我从安装建议中断@nomiclabs/hardhat-waffle得到解决方案
现在,我注意到您已经解决了您的问题,但我也想添加我的解决方案,以防万一其他人在终端中未定义部署而苦苦挣扎。 在我的情况下, namedAccounts和await getNamedAccount()很好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.