0

Let's consider the following:

struct SkillStruct{
  bytes32 skillName;
  uint index;
}

struct UserStruct {
  bytes32 userEmail;
  bytes32 userFirstname;
  bytes32 userLastname;
  uint index;
  uint amount; //amount of ECR token
  address[] userSkills;
}

mapping(address => UserStruct) private userStructs;
address[] private userIndex;

mapping(address => SkillStruct) private skillStructs;
address[] private skillIndex;

Is there a way to display all my users and theirs skills in an HTML list in my Dapp ? How can I process to get all my users ? What's the best for doing that ?

I tried to get inspired by https://www.cryptokitties.co/ source code, but all I found is a function to return kitten one by one but not all of them.

The only clue I see right now is to call a function to get users.length and then in an asynchronous loop call the getUser() function as many times as there are users. And then maybe use caching or localstorage to store them into the browser.

Is there a better way ??

Thanks u all

4
  • Is getting the information via web3 running in the user's browser a requirement or would it make more sense to have a process on your webserver that reads the blockchain and caches the results in a database? You then query the database from your webapp.
    – Victory
    Commented Dec 17, 2017 at 16:40
  • Users will perform to login via theirs MetaMask addresses which will be stored in an user struct. Once they are logged in, they will complete their profile by addind skills, firstname, lastname etc.. So I need to read the blockhchain in aim to have all the skills. And moreover, my Dapp will have a Admin page displaying all the users Commented Dec 17, 2017 at 16:47
  • I heard about using IPFS but I have no idea on it for the moment. Is that should be a way to get cheaply and quickly all my users?? Commented Dec 17, 2017 at 16:51
  • Why is not caching the information on the database work for you? That data is on the blockchain, its just needs to be accessed in a way that metamask/infura are not really designed for.
    – Victory
    Commented Dec 17, 2017 at 17:10

1 Answer 1

1

You can't return an array of structs from Solidity. Additionally, you would not want to traverse an array of structs that which could grow in size so much that gas limits would not allow the function to process.

What you could do is what you were mentioning in your question. Create a function that given an array index returns a single struct and then call that function for each element in the array (you'd probably want to do some pagination).

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.