3

Printing into console

I need to print out values like these green lines:

from ethereum.org

1) Tried returning

When I call a function that returns a string, the hexa transaction address gets printed instead of the function value.

2) Tried js

Referencing this link, I loaded the following js file in hopes to print out variable values.

var abi = /*abi interface*/
var Insurance= web3.eth.contract(abi);
var insurance = Insurance.at(web3.eth.accounts[0]);
var event = insurance.claimInsurance();

// watch for changes
event.watch(function(error, result){
    if (!error)
        console.log(result);
});
event.get(function(error, result){
    if (!error)
        console.log(result);
});

I'm getting err: TypeError: 'watch' is not a function.

Is there a way I can print out variables value from the geth console?

0

1 Answer 1

1

For achieving this you need to do 3 things:

  1. define an event in the solidity:

    event myEvent1(address _sender, string _msg);
    
  2. call this event inside from the function in solidity. in your case "listProposal()"

    myEvent1(msg.sender,"this event called")
    
  3. First, call the method in the JS using web3 object, which contains your event i.e. listProposal(). when this shall get executed in the EVM, it shall emit the event. So, you need to create a JS function, which listens to the change in the event state:

    //method call
    var abc = insurance.listProposal(42,function(err,res){...});
    
    //now you should here start watching for any event being emitted from the EVM
    var eventGenerated = insurance.myEvent1({},function(err,res){
    if(!err){
     console.log(res.args._sender+"has this message:"+ res.args._msg);// this is the line which you want to show to the console, when your message gets called, which is in green line in the console.
    }
    else{
     console.log(err);
    }
    });
    

I hope this helps. I did it using the callback method. there I write the message which you want, once your contract gets executed successfully.

Hope this helps. Thanks.

1
  • You have not used abc variable ? @Vixon
    – alper
    Commented Jul 2, 2018 at 16:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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