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!

5
  • i think you need to return it in bytes.
    – Rei Dien
    Commented Dec 29, 2017 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
    Commented Dec 29, 2017 at 18:35
  • @Jorge Can you put the code of the test you are using?
    – Ismael
    Commented Dec 29, 2017 at 19:53
  • @Jorge Having same issue. Did you get it resolved?
    – trane294
    Commented Jan 2, 2018 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
    Commented Jan 2, 2018 at 21:39

4 Answers 4

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 ...
1
  • 1
    Any idea on how to do this in solidity tests? Commented Oct 27, 2018 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);
1

You'll first have to await contract.callThatReturnsTheStruct and then access the desired property.

This does answer a 3 and a half year old question. With solc > 0.8.0 you can do something like this:

(await contract.mappingAttribute(key)).structAttribute to get the attribute of a struct that is in a mapping

0

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

0

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.