Search code examples
next.jsblockchainweb3jsnft

Cannot read properties of undefined (reading '_hex') Next js buyNft


so I am building an nft marketplace and everything is good with creating nft .. etc but when attempting to buy an nft I get this error :

ethers.umd.js?e6ac:4395 Uncaught (in promise) TypeError: Cannot read properties of undefined (reading '_hex')

Here is the code snippet:

const buyNft = async (nft) => {
    const web3Modal = new Web3Modal();
    const connection = await web3Modal.connect();
    const provider = new ethers.providers.Web3Provider(connection);
    const signer = provider.getSigner();
    const contract = new ethers.Contract(
      MarketAddress,
      MarketAddressABI,
      signer
    );

    const price = ethers.utils.parseUnits(nft.price.toString(), "ether")

console.log(price)
// code stops here
    const transaction = await contract.createMarketSale(nft.tokenId, {
      value: price,
    });

    await transaction.wait();

  };

when debugging seems that the code stops before the transaction constant. when console .log the price I get this:

enter image description here

I tried to remove the toString method, also tried to spread the price object in transaction variable like this value:{...price} but still didn't work


Solution

  • The createMarketSale() first agument expects a BigNumber instance (or a stringified number that it would convert to BigNumber).

    When you pass it undefined, it throws the error mentioned in your question.

    Solution: Make sure that your nft.tokenId is either a BigNumber or string - not undefined.