2

I am trying to understand the difference among following cases while printing the event: At web3.js

listenToEvent: function(){
    Contract.deployed().then(function(instance){
      instance.HelloWorld().watch(function(error, event){
        document.getElementById("demo").innerHTML += JSON.stringify(event);

        console.log(event); //1. It is not working
        console.log(event.args); // 2. It is giving all arguments 
        console.log(event.args.name); // 3. It is not working

      });
    });
  },

  1. console.log(event); //1. It is not working
  2. console.log(event.args); // 2. It is giving all arguments
  3. console.log(event.args.name); // 3. It is not working; here name is the first argument in the event.

My question is If the object can't be print then why it is working for 2nd condition and if it is working then why it is failing for 3rd.

To access the arguments we can use this: console.log(JSON.stringify(event.args.name));

While printing the event console.log(JSON.stringify(event)); It gives the transaction details too, how to get that?

Appreciate any help in advance.

  • Are you using web3 v1.0? You say it doesn't work what do you mean? Does it logs nothing or incorrect data? – Ismael May 15 '18 at 2:17
  • web3 v0.2 and it doesn't showing any log. – Shubham Chadokar May 15 '18 at 4:26
0

I also add issues using watch() on web3 to watch for events.

But I manage to solve them by using the alternative method (docs):

// Or pass a callback to start watching immediately
var event = myContractInstance.MyEvent([{valueA: 23}] [, additionalFilterObject] , function(error, result){
  if (!error)
    console.log(result);
});

Try changing:

instance.HelloWorld().watch(function(error, event){

to

instance.HelloWorld({}, function(error, event){
  • Thanks for the answer. Actually I am able to get the event log. I was asking what's the difference among those 3 cases which I mentioned. – Shubham Chadokar May 16 '18 at 13:50

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.