3

I want the information stored in each struct inside the mapping.

This is my struct:

struct BurnApply {
    address applicant;
    uint amount;
    string btcAddress;
    uint8 flag; 
    uint password;
}

These are the counter(to the mapping) and the mapping:

uint numRequest; //zero initialize value
mapping(uint => BurnApply) burnProcesses;

I want to obtain the values of each request inside the struct. For instance,burnProcesses[1] contain the request number 1 with all the information. How I have to call to the mapping in truffle? Because I´ve tried several ways in order to achieve that but without success. This is urgent... This is the error:

 TypeError: Cannot read property 'call' of undefined

Thanks a lot guys!

  • i think you need to return it in bytes. – Rei Dien Dec 29 '17 at 3:00
  • @ReiDien in truffle test? inside the test you have to use javascript...another thing is create your own getter function inside the smartcontract code in solidity. But, there is incompatibility converting variables in different types!!! – Jorge Dec 29 '17 at 18:35
  • @Jorge Can you put the code of the test you are using? – Ismael Dec 29 '17 at 19:53
  • @Jorge Having same issue. Did you get it resolved? – trane294 Jan 2 '18 at 8:00
  • @Ismael I´ve tried several ways of calling instance.burnProcesses.call(1), instance.burnProcesses.call({from: 1}), instance.burnProcesses(1).call (I know that this last is incorrect but just for try)...also I´ve kept away var n = 1; and then try to call using n. but the same issue. – Jorge Jan 2 '18 at 21:39
4

you can test your code using

const instance= await yourcontract.new();

const result1=await instance.BurnApply.call(i);//set your i

then

access the struct attributes using

console.log(result1[i]); //i=0 ...
| improve this answer | |
  • Any idea on how to do this in solidity tests? – Paul Razvan Berg Oct 27 '18 at 10:51
2

This contracts works for me. To access a member you have to declare it public (the compiler will create a getter automatically).

pragma solidity ^0.4.18;

contract Foo {
  struct BurnApply {
    address applicant;
    uint amount;
    string btcAddress;
    uint8 flag; 
    uint password;
  }

  mapping(uint => BurnApply) public burnProcesses;
}

Now you can call from javascript

const data = await fooInstance.burnProcesses.call(0);
| improve this answer | |
0

Call instance.burnProcesses.call(1) - you'll get a Promise returned with the item at that uint in the mapping, if it exists.

| improve this answer | |

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.