0

My contract location is here

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);    
   }

   function getInstructor() view public returns (string, uint) {
       return (fName, age);
   }

}

MY JS script

window.addEventListener('load', function() {
    if (typeof web3 !== 'undefined') {
        console.log('Web3 Detected! ' + web3.currentProvider.constructor.name)
        window.web3 = new Web3(web3.currentProvider);
    } else {
        //console.log('No Web3 Detected... using HTTP Provider')
        window.web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
        //TO-DO
        //Pop Up Model And Ask User download MetaMask       
        console.log('No web3? You should consider trying MetaMask!');
    }
    startApp();
});


function startApp() {
    var CoursetroContract = web3.eth.contract(
        [{ "constant": false, "inputs": [{ "name": "_fName", "type": "string" }, { "name": "_age", "type": "uint256" }], "name": "setInstructor", "outputs": [], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "constant": true, "inputs": [], "name": "getInstructor", "outputs": [{ "name": "", "type": "string" }, { "name": "", "type": "uint256" }], "payable": false, "stateMutability": "view", "type": "function" }, { "anonymous": false, "inputs": [{ "indexed": false, "name": "name", "type": "string" }, { "indexed": false, "name": "age", "type": "uint256" }], "name": "Instructor", "type": "event" }]
    );

    var Coursetro = CoursetroContract.at('0x96d2a2978d52287c2DffF0e2A40D12181223303C');

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



    instructorEvent.watch(function(error, result) {
        if (!error) {
            $("#loader").hide();
            $("#instructor").html(result.args.name + ' (' + result.args.age + ' years old)');
        } else {
            $("#loader").hide();
            console.log(error);
        }
    });

    $("#button").click(function() {
        $("#loader").show();
        Coursetro.setInstructor($("#name").val(), $("#age").val(), function(error, result) {
            if (!error) {


            } else
                console.log(error);
        });
    });
}

When I click the button to call setInstructor function and then the transaction is done, the Event is not triggered. Please Help.

0

If you want to watch event and not get the historic events, you should change your event declaration to:

var instructorEvent = Coursetro.Instructor();

Otherwise it would look like this:

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

myEvent.get((error, events) => {
    if (!error) {
        function ShowResults(event) {
            console.log('Argument 1: ' + event.args._arg1);
            console.log('Argument 2: ' + event.args._arg2);
        }
        events.forEach(ShowResults);
    } else {
        console.log('Error');
    }
});
  • It's not the exact answer. However, it really helps me a lot. I finally use the getTransactionReceipt function to do what I want. Thanks. – shawn Feb 18 '18 at 17:55

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.