0

I have deployed a contract RegisterContract

pragma solidity ^0.4.0;

contract RegisterContract{

struct RegStruct{
    uint regno;
    string name;
    string dob;
    uint contact;
}

RegStruct[] private regStruct;
uint Index; 

function set(uint _regno,string _name,string _dob, uint _contact) public 
returns(uint Index){
    Index=regStruct.length;
    regStruct.push(regStruct(_regno,_name,_dob,_contact));
}

function getCount() public constant returns(uint count) {   
    return regStruct.length;
}
function get(uint Index) public constant returns(string name,uint regno,string dob){
    return(
        regStruct[Index].name,
        regStruct[Index].regno,
        regStruct[Index].dob
    );
}
}

when i register details by calling set() it works fine(i think so because when set() is called transaction take place in metamask ). But when i call getCount(), which shows number of entries in the contract, return 0. is it because the set() doesn't work properly? can someone help me

JS code:

<script type="text/javascript" src="/web3.js/dist/web3.js"></script>
<script type="text/javascript">
var Web3 = require('web3');
    if (typeof web3 !== 'undefined') {
        web3 = new Web3(web3.currentProvider);
    }
    else {
        web3 = new Web3(new Web3.providers.HttpProvider("http://192.168.1.103:8545"));
    }

var MyContract = web3.eth.contract([ { "constant": false, "inputs": [ {"name": "_regno", "type": "uint256" },{ "name": "_name", "type": "string" }, { "name": "_dob", "type": "string" }, { "name": "_contact", "type": "uint256" } ], "name": "set", "outputs": [{ "name": "Index", "type": "uint256", "value": "0" }], "payable": false, "type": "function" }, { "constant": true, "inputs": [], "name": "getCount", "outputs": [ { "name": "count", "type": "uint256", "value": "0" } ], "payable": false, "type": "function" }, { "constant": true, "inputs": [ { "name": "Index", "type": "uint256" } ], "name": "get", "outputs": [ { "name": "name", "type": "string", "value": "" }, { "name": "regno", "type": "uint256", "value": "" }, { "name": "dob", "type": "string", "value": "" } ], "payable": false, "type": "function" } ]);    
var MyContractInstance=MyContract.at('0xB5eCAd99ba935C0F30Df08AAaFDAED3c7a0E641c');
function insert()
{
    MyContractInstance.set(document.getElementById('regno').value,document.getElementById('name').value,document.getElementById('dob').value,document.getElementById('cntct').value,{ from: web3.eth.coinbase},
    function (err, result) {console.log("result1 : "+result);});
  alert("Registered ok");
  parent.location='home.html';
}

MyContractInstance.getCount(function (err, cnt_result){
    console.log("result1 : "+cnt_result); 
    len=cnt_result;

    console.log("length:"+len);
    for(var i=0;i<len;i++){
        MyContractInstance.get(i,function (err, reg_result){
            console.log("result2 : "+reg_result);
            var table = document.getElementById("myTable");
            var row = table.insertRow(myTable.rows.length);
            var cell1 = row.insertCell(0);
            var cell2 = row.insertCell(1);
            var cell3 = row.insertCell(2);
            cell1.innerHTML = reg_result[0];            //name
            cell2.innerHTML = reg_result[1];        //regno
            cell2.innerHTML = reg_result[2];
        });
    }
});

</script>
1

If you run this on Remix (https://remix.ethereum.org), you'll see there's a few errors that might be causing the issue.

1- You get 2 warning for the declarations of the returns values Index as they are called the same as the uint Index propery.

2- Most importantly, inside the get function you are getting an error as you are trying to return petStruct variable which doesn't exist at all. Did you mean to return regStruct?

EDIT after JS code was added:

You are missing the .call() method. Every function whose purpose is to read data must use .call()

Your function should be written as:

MyContractInstance.getCount.call(function(error, value) {

}); 
| improve this answer | |
  • yes i mean to return regStruct. When i run this on Remix it works fine(even though there are some warning ). but i want to call these functions using web3 in html page – user8106215 Oct 4 '17 at 13:39
  • If the contract is running properly on Remix and you are having problems on the browser, please upload your JS code as there might be a problem there, then. – pabloruiz55 Oct 4 '17 at 13:58
  • my JS code is uploaded – user8106215 Oct 4 '17 at 15:34
  • Just updated my answer. – pabloruiz55 Oct 4 '17 at 16:01
  • i updated my call function, yet getCount returns 0 – user8106215 Oct 4 '17 at 16:11

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.