2

I'm following this very simple web3 tutorial and Event.watch doesn't seem to fire when I click the button.

I can't figure it out.

Can someone please help?

pragma solidity ^0.4.18;

contract Coursetro {

   string fName;
   uint age;

   event Instructor(
       string name,
       uint age
    );

   function setInstructor(string _fName, uint _age) public {
       fName = _fName;
       age = _age;
       Instructor(_fName, _age); 
   }
}

and then JS:

var instructorEvent = Coursetro.Instructor();
instructorEvent.watch(function(error, result){
  alert(1);
});
$("#button").click(function() {
  Coursetro.setInstructor('a', 1);
});

alert never happens.

  • Are you sending a transaction to setInstructor and if so, is it being mined? – Malone Nov 19 '17 at 17:48
  • What do you mean by sending a transaction? And this is executed on a local testrpc. – vlad Nov 19 '17 at 18:33
3

I had a similar issue and solved it by specifying the block range given by fromBlock and toBlock when defining the reference to the event in JS. So for your case, you could try the following:

var instructorEvent = Coursetro.Instructor({}, {fromBlock: startBlockNo, toBlock: 'latest'});
instructorEvent.watch(function(error, result){
    alert(1);
});

Note that only the first line is changed, I added the rest just for completeness.

  • @gisdev_p Where are you populating startBlockNo from? – Damian Green Dec 21 '17 at 16:25
  • You can choose it freely, as needed. Some examples: You could set it to the block with that you deployed your smart contract, or to a block that corresponds to a certain date or that covers a certain period in the past. In an case, has to be smaller or equal to the total number of blocks, of course. – gisdev_p Dec 21 '17 at 17:24

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.