0

I'd like to get the event result in my UI.

Here is my contract

pragma solidity ^0.4.4;


 contract T2{
 uint public k;
 event CheckVal(uint val);
 function Test() returns (uint){        
  var temp=441;
  CheckVal(temp);
  k=32; 
  return temp;
   }    
}

and in the console I have this:

var eve=instance.CheckVal();
eve.watch(function(error, result){console.log( 
result.args.val.toNumber());});

Every time I run

    instance.Test({from:eth.accounts[0],gass:4200000})

441 appears in the console, which is right. But I want to get the value (i.e. 441) in my UI, so I want to assign it to a variable.

Question: How to assign the output of event log to a variable?

  • assign result.args.val.toNumber() to your variable? – d9ngle Sep 25 '17 at 14:13
1

This question can have a lot of different answers based on what you want to do with the variables (like do you want to overwrite it, or store a growing list of variables that come in through your event?), but the simplest version would be like this.

var eve=instance.CheckVal(); var storedValue; eve.watch(function(error, result){ storedValue = result; });

And now if you log out storedValue, it will return 441.

1

It's as simple as this:

var eventOutput
var eve = instance.CheckVal();
eve.watch(function(error, result){
  eventOutput = result.args.val.toNumber()
});

Of course you need to use that output in some way in your frontend to make it visibile, e.g. by saving it in the state if this is a React app.

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.