简体   繁体   中英

TypeError: Cannot read property 'length' of undefined - While deploying using hardhat

I am getting the following error while trying to deploy a Mock contract on the hardhat development chain.

I am following:

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

I got this error while I was at 10:48:53 of the above course. I tried almost every thing but nothing helped but I found where the problem was occurring.

Ques: Where do I think the problem is occurring? (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.

Here is the error:

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 % 

Here is the code snippet in which I am getting the error:

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"];

Please help me with this problem I posted this question on Ethereum Stack Exchange but got no help.

System configuration: MacBook Air M1 2021 - Monterey 12.4, yarn: 1.22.18, Hardhat: 2.9.6

I got the answer from the MASTER himself, @PatrickAlphaC.

When we call await getNamedAccounts in the above code, hardhat looks for the length of the named accounts in our hardhat.config.js

We have to make sure that the property given below is in there!

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

This solved my problem and I was HAPPY again!

Make sure you have await in getNamedAccount (). Also name must be correct.

Must have correct entry for named accounts as below:

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

I was stuck for a while with this as well, and was also following this same course.

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)

Initially I was writing from scratch and kept getting this error and plus hardhat npm installation. I decided to test just in case the course code, so I cloned the repo and surprisingly was also struggling with npm install issues for hardhat and waffle.

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.

So fixing dependency hardhat-ethers by overriding it, fixed all issues for me, so try this:

"devDependencies": {
  ...
   "@nomiclabs/hardhat-ethers": "npm:hardhat-deploy-ethers@^0.3.0-beta.13",
  ...
 },
 "overrides": {
    "@nomiclabs/hardhat-waffle": {
    "@nomiclabs/hardhat-ethers": "$@nomiclabs/hardhat-ethers"
  }
 }

I got solution from Installation advice breaks @nomiclabs/hardhat-waffle

Now, I noticed that you have already solved your problem, but I wanted to add my solution as well just in case if someone else is struggling with deploy being undefined in terminal. In my case namedAccounts and await getNamedAccount() was fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM