1

Consider the following contract:

contract Test {  
  struct person {  
    uint id;
    string name;
  }

  person[] p;
  function addPerson (string _name) returns (bool added) {  
    person memory per;
    per.id = msg.sender;
    per.name = _name;
    p.push[per];
  }

  function getPerson (address a) constant returns (address d, string n) {  
    for (uint k = 0; k <= k++) {  
      if (p[k].id == a)  {
        d = p[k].id;
        n = p[k].name;
      }
    }
  }
}  

Upon making a web3 based sendTransaction to the 'addPerson' function, a transaction hash is produced. While retrieving the output of the constant function using eth.call as below:

TestObject.getPerson("0x1234", function(error, result){  
     if(!error)  
         console.log(result)  
     else  
         console.error(error);  
 });

the result is: ['0x','0x']

When the tested the same in the online solidity compiler, it results in the actual values passed in during the sendTransaction interaction. Any suggestions,as to how to get around this situation, to retrieve the actual results?

2

There were some unexpected problems in the contract that prevented it from actually compiling.

Near line 6 - id needs to be an address because you're storing msg.sender there.

I inserted p.length in for (uint k = 0; k <= k++) {.

Changed up the way values are returned and added an event log to make it easier to see what's going on.

The way you're iterating to find a match isn't ideal. Skipping ahead a bit, but this might help out. Are there well-solved and simple storage patterns for Solidity?

Hope it helps.

pragma solidity ^0.4.6;

contract Test {

  struct person {  
    address id;
    string name;
  }

  person[] p;

  event LogNewPerson(address newPerson, string name);

  function addPerson (string _name) returns (bool added) {  
    person memory per;
    per.id = msg.sender;
    per.name = _name;
    p.push(per);
    LogNewPerson(msg.sender, _name);
    return true;
  }

  function getPerson (address a) constant returns (address d, string n) {  
    for (uint k = 0; k <= p.length; k++) {  
      if (p[k].id == a) {
        return(p[k].id, p[k].name);
      }
    }
  }
} 

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.