3

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

eth
  • 85,679
  • 53
  • 285
  • 406
Sergey Potekhin
  • 1,360
  • 2
  • 13
  • 28
  • 1
    The last example in the JS console looks like it should work. Maybe this question needs to focus on that part. – eth Apr 05 '17 at 23:43
  • @eth Yep, it works fine, but I get txn hash instead of 123, as expected – Sergey Potekhin Apr 05 '17 at 23:47
  • 1
    Sorry, I misread the console output as error being undefined and data as null. The txn hash is returned because a transaction was issued by web3 instead of a "call" (hope you saw the question that asks about the difference). – eth Apr 06 '17 at 04:28

2 Answers2

4

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?

eth
  • 85,679
  • 53
  • 285
  • 406
3

Did you try using the call function asynchronously:

contract.getData.call().then(function(result) {
  console.log(result);
});
TyndieRock
  • 171
  • 6
  • I've had pretty good success using Promise.promisifyAll (after contract instantiation) so you can use actual return values (Promises) instead of callbacks, if return values are important to you. – Paul S Apr 06 '17 at 07:30