1

I am creating a contract of storing all the names and address of the users.

Then I returning the array of the names and address.

contract demo{

    address public user;
    uint  public  i=0;

    function demo(){
        user=msg.sender;
    }

    address[10] users;
    bytes32[10] name;

    function add(bytes32 _name){

        users[i]=user;
        name[i]=_name;
        i++;

    }

    function get_names() returns(bytes32[10]){
        return name;
    }

    function get_address() returns(address[10]){
        return users;
    }
  }

when I call the get_names function using web3, it did not returning the names of all the added users.

please help me what to do...

here is the code of web3

mycontract=web3.eth.contract(ABIArray).at(contract_Address);

    console.log(mycontract);
    var defaultAccount = web3.eth.defaultAccount;

    mycontract.get_names(function(err,res){

      console.log("get_names "+res);
      TemplateVar.set(template, "counter", res);

    })

     mycontract.get_address(function(err,res){



      console.log("get_address "+res);
      TemplateVar.set(template, "counter1", res);

    })


      mycontract.i(function(err,res){
        console.log("i "+res);
      TemplateVar.set(template, "counter2", res);

    })

And the returned value is not an array of names or address.

the returned value is printed

1 Answer 1

2

The main thing you are missing is the keyword constant for the functions returning the data. Here's your modified contract code that should work:

pragma solidity ^0.4.8;

contract Demo {
    address public user;
    uint public i = 0;

    function Demo() {
        user=msg.sender;
    }

    address[10] users;
    bytes32[10] name;

    function add(bytes32 _name) {
        users[i] = user;
        name[i] = _name;
        i++;
    }

    function get_names() constant returns(bytes32[10]) {
        return name;
    }

    function get_address() constant returns(address[10]) {
        return users;
    }
}
1
  • But when i call get_names using web3 , it gives "0x3035f432129b794ba9907232ccadbc138280a6355d3a573f4255407bf9255fe7" as a result. It is giving TxHash of the transaction. How do I get the value of the array name. Mar 29, 2017 at 9:43

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.