Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Blockchain by (12.7k 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 (29.5k points)
edited by

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.

If you want to be an expert in your field, then enroll in Blockchain Certification by Intellipaat.

Related questions

0 votes
1 answer
asked Jul 27, 2019 in Blockchain by Abhishek_31 (12.7k points)
0 votes
1 answer
0 votes
1 answer

Browse Categories

...