0

I get this error Unhandled Runtime Error TypeError: Cannot read properties of undefined (reading 'args') whenever I want to create an nft on my decentralised application and it only appears after I confirm the payment on metmask then it just pops up and I have no idea why it keep occurring. here is the code that I get the error from:

async function createSale() {

    const url = await createItem();
    const web3Modal = new Web3Modal();
    const connection = await web3Modal.connect();
    const provider = new ethers.providers.Web3Provider(connection);
    const signer = provider.getSigner();

    /* next, create the item */
    let contract = new ethers.Contract(nft, NFT.abi, signer);
    let transaction = await contract.createToken(url);
    let tx = await transaction.wait();

    let event = tx.events[0];
    let value = event.args[2];
    let tokenId = value.toNumber();
    const price = ethers.utils.parseUnits(formInput.price, "ether");
    contract = new ethers.Contract(
      marketplaceAddress,
      NFTMarketplace.abi,
      signer
    );
    let listingPrice = await contract.getListingPrice();
    listingPrice = listingPrice.toString();
    transaction = await contract.createMarketItem(tokenId, price, {
      value: listingPrice,
    });
    await transaction.wait();
    router.push("/");
  }

Here is the new code with printing events:

async function createSale() {
  const url = await createItem();
  const web3Modal = new Web3Modal();
  const connection = await web3Modal.connect();
  const provider = new ethers.providers.Web3Provider(connection);
  const signer = provider.getSigner();

  /* next, create the item */
  let contract = new ethers.Contract(nft, NFT.abi, signer);
  let transaction = await contract.createToken(url);
  let tx = await transaction.wait();

  if (!tx.events || tx.events.length === 0) {
    console.error("No event found in the transaction.");
    return;
  }

  let event = tx.events[0];

  if (!event.args || event.args.length < 3) {
    console.error("Event args missing or insufficient.");
    return;
  }

  let value = event.args[2];
  let tokenId = value.toNumber();
  const price = ethers.utils.parseUnits(formInput.price, "ether");
  contract = new ethers.Contract(
    marketplaceAddress,
    NFTMarketplace.abi,
    signer
  );
  let listingPrice = await contract.getListingPrice();
  listingPrice = listingPrice.toString();
  transaction = await contract.createMarketItem(tokenId, price, {
    value: listingPrice,
  });
  await transaction.wait();
  router.push("/");
}

I get this output "No event found in the transaction."

11
  • 1
    print the args and see whether there is some value in it or not May 8 at 4:32
  • i did print and there is no value
    – Eyad Ahmed
    May 21 at 12:45
  • Print eventts and check whether there is any propert named args or not. Sometimes the property name changee. For example it might be arg or arguments. once print the event variable and check for the property you want. May 21 at 13:58
  • Here is the new code that I wrote to print events:
    – Eyad Ahmed
    May 21 at 16:40
  • I perosnally do not know how to fix this error and complete my metamask transaction
    – Eyad Ahmed
    May 21 at 16:50

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.