0 votes
1 view
in Blockchain by (11.8k points)

I'm trying to watch for an event on my frontend but a bug is getting in the way.

Here's the JS watching for the event

import web3 from './web3';

export async function callEvent () {

  await Contract.events.PracticeEvent().watch((response) => {

    console.log('the event has been called', response);

  }).catch((err) => {

    console.log(err);

  })

   await Contract.triggerEventFunc().call();

Contract Code:

event PracticeEvent (string _message, uint _timestamp);

function checkEvent() public {

    emit PracticeEvent("event has been called", gts);

}

---web3.js file

import Web3 from 'web3';

const web3 = new Web3(window.web3.currentProvider);

export default web3;

So when I run the app I get an error saying

"Uncaught (in promise) TypeError: Contract.default.events.PracticeEvent(...).watch is not a function"

This all works fine in Remix but it gets messed up when I try run it in my actual app

I'm assuming the bug has something to do with web3 but I'm not sure why because the web3 stuff is working fine in the rest of my app.

Any help? thanks!

1 Answer

0 votes
by (27.7k points)

Events can only be emitted inside transactions. You're doing a .call(), which just reads data from the blockchain, and can't emit an event.

If you want the function to emit the event you should replace

await Contract.triggerEventFunc().call();

with

await Contract.triggerEventFunc().sendTransaction();

This will send a transaction to the blockchain, and it will cost ether to send. Inside this transaction, an event can be emitted, so you should be able to catch it with web3.

Related questions

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...