Ethers.js – Resolving TypeError: Cannot Read Properties of Undefined (Reading ‘0’)

ethers.js

I am running the propose.ts to execute the propose function from Governor contract the args are transacted on the local blockchain as I retrieved the logs from the transaction event.

EventLog {
    fragment: EventFragment {
      type: 'event',
      inputs: [Array],
      name: 'ProposalCreated',
      anonymous: false
    },
    args: Result(9) [
      71868859171677419052155345699497483540960066113331982602962523540651890624828n,
      '0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266',
      [Result],
      [Result],
      [Result],
      [Result],
      16n,
      21n,
      'This proposal shall execute the box store function to save the uint256 passed through proposal'
    ]
  }
]

Here's the code according to the tutorial video though I am using ethers v6. proposeReceipt.events[0].args.proposalId returns error

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

const proposeTx = await governor.getFunction("propose")(
    [await box.getAddress()],
    [0],
    [encodedFunctionCall],
    proposalDescription
  )
  // If working on a development chain, we will push forward till we get to the voting period.
  if (developmentChains.includes(network.name)) {
    await moveBlocks(VOTING_DELAY + 1)
  }

  const proposeReceipt = await proposeTx.wait(1);
  const proposalId = proposeReceipt.events[0].args.proposalId
  console.log(`Proposed with proposal ID:\n  ${proposalId}`)

Best Answer

The receipt has the events listed under the logs key and not events. This should work:

const log = governor.interface.parseLog(proposeReceipt.logs[0]);
const proposalId = log.args.proposalId;
Related Topic