PatrickAlphaC / hardhat-nft-marketplace-fcc

114 stars 97 forks source link

TypeError: Cannot read properties of undefined (reading '0') #33

Closed dashDebasis123 closed 1 year ago

dashDebasis123 commented 1 year ago

While building a nftmarketplace , I got an error in running the mint-and-list.js in localhost

code for BasicNftTwo.sol ........................................................

// SPDX-License-Identifier: MIT pragma solidity ^0.8.7; import "@openzeppelin/contracts/token/ERC721/ERC721.sol"; contract BasicNftTwo is ERC721 { string public constant TOKEN_URI = "ipfs://QmdryoExpgEQQQgJPoruwGJyZmz6SqV4FRTX1i73CT3iXn"; uint256 private s_tokenCounter; event DogMinted(uint256 indexed tokenId); constructor() ERC721("Dogie", "DOG") { s_tokenCounter = 0; } function mintNft() public { _safeMint(msg.sender, s_tokenCounter); emit DogMinted(s_tokenCounter); s_tokenCounter = s_tokenCounter + 1; } function tokenURI(uint256 tokenId) public view override returns (string memory) { require(_exists(tokenId), "ERC721Metadata: URI query for nonexistent token"); return TOKEN_URI; }

function getTokenCounter() public view returns (uint256) {
    return s_tokenCounter;
}

} ..............................................................................................................

code for mint-and-list.js ............................................................................................................... const { ethers, network } = require("hardhat") const { moveBlocks } = require("../utils/move-blocks")

const PRICE = ethers.parseEther("0.1")

async function mintAndList() { const nftMarketplace = await ethers.getContract("NftMarketplace") // const randomNumber = Math.floor(Math.random() * 2) const basicNft = await ethers.getContract("BasicNftTwo")

// } else {
//     basicNft = await ethers.getContract("BasicNft")
// }
console.log("Minting NFT...") 
const mintTx = await basicNft.mintNft()
console.log(mintTx)
const mintTxReceipt = await mintTx.wait(1)
// console.log(mintTxReceipt)

const tokenId = mintTxReceipt.events[0].args.tokenId
// console.log(tokenId)

console.log("Approving NFT...")
const approvalTx = await basicNft.approve(nftMarketplace.address, tokenId)
await approvalTx.wait(1)
console.log("Listing NFT...")
const tx = await nftMarketplace.listItem(basicNft.address, tokenId, PRICE)
await tx.wait(1)
console.log("NFT Listed!")
if (network.config.chainId == 31337) {
    // Moralis has a hard time if you move more than 1 at once!
    await moveBlocks(1, (sleepAmount = 1000))
}

}

mintAndList() .then(() => process.exit(0)) .catch((error) => { console.error(error) process.exit(1) }) .................................................................................................

getting error TypeError: Cannot read properties of undefined (reading '0')

PatrickAlphaC commented 1 year ago

Hello! Thanks for making this issue.

Can you:

  1. Make this a discusson on the full repo? https://github.com/Cyfrin/foundry-full-course-f23/discussions

  2. Could you please remember to follow this section to format your questions? https://youtu.be/umepbfKp5rI?t=22464