1

I am trying to read a mapping inside my smart contract that looks like this:

mapping(address => User) public users;

My web3js code looks like this. I am trying to read mapping users inside the smart contract:

var contractAddress='0x3aacc70dced50f8c6c1951f27d505d2ad165b373';
var contractInstance = new web3.eth.Contract(abi,contractAddress);
contractInstance.users.call(web3.eth.accounts[0], function(err, result){
 if(!err){
    alert(result)
 }
 });

However I get the error like below:

TypeError: Cannot read property 'call' of undefined

Can someone help me resolve this

1

You can't call a mapping public getter without sending a key with it. Also you are returning a Struct which is not possible yet (AFAIK) check this answer for more : https://github.com/trufflesuite/truffle/issues/302

If that is not fix your problem, you may need to check your .json built by truffle or try to deploy again.

0

When arrays are returned with public getters, you need to specific the index that you are looking for.

You may find the following article useful structs from public functions and/or just manually loop through each index and add it to a json object.

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.