Could somebody please provide me code that works to listen to an event from your front-end. It was working for me and there are a few tutorials that I've tried now to fix it but it still persists with the wrong behaviour.

I call a function from the front-end, which then emits an event, which should then trigger the front-end listener. However instead it does not trigger it. I wait for awhile it never comes, so I call the function again, and instantly the front-end event listener sees the previous event and triggers on it. I'm not sure why this is happening now, if anybody could give me code that works, that would be great. This was simple and was working three days ago, also it was still working after I updated to the latest version of Truffle, and web3 1.0.

Below is my code, it is very simple, and standard, all it does it emit the event. Any help would be amazing, thanks! I've asked a few other questions similar to this, but have now realized it has nothing to do with Oraclize etc. it is purely simply just my event not being heard until the next block/function call happens.

Here is my front-end event listener

var event = contractInstance.Instructor({}, {fromBlock: 0, toBlock: 'latest'});

      event.watch(function(error, result) {

Here is my contract code that emits the event

event Instructor(
  string name,
  uint age

function setInstructor(string _fname, uint _age) public {
    fName = _fname;
    age = _age;
    /*emit*/ Instructor(_fname, _age); // doesn't properly work with or without emit

It calls the contract successfully, the only problem is listening to the event that gets emitted. Thanks to any help!!

*Edited to add my code

  • Showing your code would really help in this situation. Can't specifically help if you don't provide anything. How to ask a question. – ReyHaynes Apr 3 '18 at 1:47
  • @ReyHaynes I've added the code now I believe is important to this question (everything else is working). Please let me know if I should add anything else, thanks! – savard Apr 3 '18 at 1:59
  • Can you try inline chaining the event: contractInstance.Instructor().watch((err, res) => console.info(res)) to see if that works. – ReyHaynes Apr 3 '18 at 16:50
  • @ReyHaynes thanks for the reply! This is going to sound crazy but I used the code you provided and it worked. I was super happy, so I closed it and ran it again to make sure, and it hasn't worked since. It worked the first time I tried it, and now it's doing the same behaviour as my previous code, which is no event shows up for awhile, so I push the button again, and the event will instantly show up. Does this sound like anything you have heard of before? – savard Apr 4 '18 at 6:16
  • @ReyHaynes interesting behaviour I just discovered. If you test your DApp with MetaMask for transactions, you need to 'reset' the account each time so the nonce is correct for your private blockchain on Ganache (or wherever). When I reset that then without refreshing the page, the events never work properly, but if I refresh the page after or do the reset before opening the page on a localhost, the events work perfectly, with your code or my original code. I'm happy to have discovered this, but is that considered a bug? It never acted this way before about 3-4 days ago. – savard Apr 4 '18 at 6:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.