0
pragma solidity ^0.4.23;

contract ChainList  {

  struct  data  {
    uint   ownernumber;
    uint   id;
  }

  uint value;
  mapping (uint256 => data) public  datamatching;

  function storedata (uint _ownernumber, uint _id) public {
    var  persondata  = datamatching[value];
    persondata .ownernumber = _ownernumber;
    persondata. id  =  _id;
  }

  function getData(uint256 userId) returns (uint, uint){
    return (datamatching[userId].ownernumber, datamatching[userId].id);
  }
}

// I have written the function but I tought that when I call the get function it will return all the child varables but it is not working

truffle(development)> app.storedata(123344,1)
{ tx: '0x1fad231de1e19b004c8004871036a7cf47be06ab4123614130f1b1371371433b',
  receipt: 
   { transactionHash: '0x1fad231de1e19b004c8004871036a7cf47be06ab4123614130f1b1371371433b',
     transactionIndex: 0,
     blockHash: '0xbff254564dc0d2e8affc80ca5994a66be5c06377f98ff425783c9b02fd36c75c',
     blockNumber: 20,
     gasUsed: 62379,
     cumulativeGasUsed: 62379,
     contractAddress: null,
     logs: [],
     status: 1 },
  logs: [] }
truffle(development)> app.getData(123344)
{ tx: '0x327089e695085696d00c5656db322bce490bc17bf0edaa19f17dad38cdf74377',
  receipt: 
   { transactionHash: '0x327089e695085696d00c5656db322bce490bc17bf0edaa19f17dad38cdf74377',
     transactionIndex: 0,
     blockHash: '0x85ccd6ccc26417365681525442b0349ce29a1a46b9de4184ca1f1e3ec4b66fd4',
     blockNumber: 21,
     gasUsed: 22454,
     cumulativeGasUsed: 22454,
     contractAddress: null,
     logs: [],
     status: 1 },
  logs: [] }
truffle(development)> 
undefined
truffle(development)> 
undefined
truffle(development)> app.storedata(123344,3)
{ tx: '0xafb132e70bfb82e1d41227bf41d787faaf6ee9d99c63fa3145752e2178b5a4e6',
  receipt: 
   { transactionHash: '0xafb132e70bfb82e1d41227bf41d787faaf6ee9d99c63fa3145752e2178b5a4e6',
     transactionIndex: 0,
     blockHash: '0x61dacd6a0a2d48627b3becc2a815ec7b72a7fe175497ef66964dffdd3bdf2c6f',
     blockNumber: 22,
     gasUsed: 32379,
     cumulativeGasUsed: 32379,
     contractAddress: null,
     logs: [],
     status: 1 },
  logs: [] }
truffle(development)> app.getData(123344)
{ tx: '0x63c83f31d384494ed8ad6389c43d2a5745a6e229242c5fb6218386b3387d6409',
  receipt: 
   { transactionHash: '0x63c83f31d384494ed8ad6389c43d2a5745a6e229242c5fb6218386b3387d6409',
     transactionIndex: 0,
     blockHash: '0x7c3252574ffcd2f65f207b279d1b7501655e2638ddd52397c8fde93a9be4cb00',
     blockNumber: 23,
     gasUsed: 22454,
     cumulativeGasUsed: 22454,
     contractAddress: null,
     logs: [],
     status: 1 },
  logs: [] }

When I call getData with id 123344 it should return the two values 1 and 3 because I used the same id to store the both varibles but it is showing a different hash.

I want to getdata to show the hash of the two transactions id's and the values 1 and 2 when user calls it.

2

The issue in your contract is in the line:

var  persondata  = datamatching[value];

This line should be:

var  persondata  = datamatching[_id];



Note: I would do: data storage persondata = datamatching[_id];

EDIT

Another issue is that you are using truffle and you want to get the returned value of the function getData. Truffle will only give you teh result of the transaction.

Instead, you can use .call() at the end of your function in truffle to get the value of the variable.

Hope this helps

  • HI can you plese explain what is data storage – Venkatesh Muthyala Oct 2 '18 at 5:26
  • data Is the name you give to the struct. Storage means that the variable persondata is a pointer to a variable in the storage of the contract and is of type "data" as defined in the struct. If this solved your question, please accept the answer. – Jaime Oct 2 '18 at 6:45
  • sorry james On paper it seems correct but when compiling i still not getting the desired output (so i am trying to use events ) if you don't mind can you check once in your machine But Thanks a lot for your support i want to give upvote but i can't due to low score . Hope you Understand – Venkatesh Muthyala Oct 2 '18 at 7:16
  • what do you mean by not getting the desired output? I tested it on remix, I save the data with storedata and then I am able to retrieve it, please explain what is that you do not get. Also, why are you talking about events? – Jaime Oct 2 '18 at 7:18
  • I have deployed using truffle and stored in a app varable app.storedata(123344,1) { tx: '0x6b5188860f04df168e2b36c45dfbdde78f7303c416c0918e3f4fa52cf931ceff', receipt: { transactionHash: '0x6b5188860f04df168e2b36c45dfbdde78f7303c416c0918e3f4fa52cf931ceff', transactionIndex: 0, blockHash: '0x0682f6a263654e005c4dd86e92cf905faa5ae061ebdc8e0ad27132e7e411c3da', blockNumber: 5, gasUsed: 62179, cumulativeGasUsed: 62179, contractAddress: null, logs: [], status: 1 }, logs: [] } – Venkatesh Muthyala Oct 2 '18 at 7:27

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.