开发者问题收集

TypeError:无法读取未定义的属性“长度”-使用安全帽部署时

2022-06-01
3818

我在尝试在安全帽开发链上部署模拟合约时收到以下错误。

我正在关注:

Learn Blockchain, Solidity, and Full Stack Web3 Development with JavaScript – 32-Hour Course by Patrick Collins on FreeCodeCamp YT channel

我在上述课程的 10:48:53 时收到此错误。我几乎尝试了所有方法,但都无济于事,但我找到了问题发生的地方。

问:我认为问题发生在哪里? (12:26)

According to me the problem is at the line where we are trying to deploy the script and we are waiting for the promise to be returned.

错误如下:

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

3个回答

我从大师本人 @PatrickAlphaC 那里得到了答案。

当我们在上面的代码中调用 await getNamedAccounts 时,hardhat 会在我们的 hardhat.config.js 中查找命名帐户的长度

我们必须确保下面给出的属性在那里!

namedAccounts: {
    deployer: {
        default: 0, // here this will by default take the first account as deployer
    },
},

这解决了我的问题,我又很高兴了!

Rishav Sharma
2022-06-02

确保在 getNamedAccount () 中已 await。名称也必须正确。

必须具有正确的命名帐户条目,如下所示:

namedAccounts: { deployer: { default: 0, deployer 1: 0, }, },

Sachin
2022-06-16

我也被这个问题困扰了一段时间,也遵循了同样的过程。

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 安装。我决定测试一下课程代码,因此我克隆了 repo,令人惊讶的是,我还在为 hardhat 和 waffle 解决 npm install 问题。

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/[email protected]
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/[email protected]
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() 都没问题。

Ilimkan Omurzakova
2022-08-05