0

I want to get the value that my smart contract returns in javascript.

Here is the contract. (version 0.4.25 - web3 version 1.2)

pragma solidity ^0.4.25;
contract SimpleStorage {

    uint public storedData;
    event numberSaved(uint number);

    constructor(uint initVal) public {
        storedData = initVal;
    }

    function set(uint x) public{
        storedData = x;
        emit numberSaved(storedData);
    }

    function get() view public returns (uint retVal) {
        return storedData;
    }

}

Now how do I get the value the event returned?

I tried with:

mySmartContract.events.numberSaved();

Returns object

mysmartContract.once('numberSaved', function(error, event){ console.log(event); });

This one returns null

What am I doing wrong?

It works in Remix. Remix returns the value

0

Try this

mysmartContract.getPastEvents('numberSaved', {
    fromBlock: 0,
    toBlock: 'latest'
}, function(error, events){ console.log(events); })
.then(function(events){
    console.log(events) // same results as the optional callback above
});

Actually, I took the example from Web3 docs.

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.