web3.js – Best Way for Web3 to Receive Data from Function Return

contract-developmentdapp-developmentjson-rpcmetamaskweb3js

Now I'm trying to delelop my first Dapp (using Metamask plugin). everything is going to be all right, but I can't get one thing: Is it possible to receive return from function, using Web3 (not JS VM!)

For example, I have a simple contract like this:

pragma solidity ^0.4.0;

contract test {
    int32 data = 123;

    function getData() returns (int32) {
        return data;
    }
}

When I'm calling getData() with JS virtual machine, it works fine:

remix

But with selected Inject web3 (Metamask) it returns information about new transaction, instead of 123

remix_web3

I also can't receive any data from the JS console (Metamask):

enter image description here

Best Answer

For the question, the simplest is to make getData constant as:

function getData() constant returns (int32)

Another option is to use an event: How to get return values when function with argument is called?