0

I am having trouble accessing data from a mapping that I have created on the client side.

My smart contract has 2 function 1 that sets the values for the mapping. The second one retrieves the values of my mapping. When I deploy and run these functions in Remix they return the correct value. But when I start to integrate then with my front end I am hitting some issues.

Here are the smart contract functions:

struct Student {
    string grade;
    address studentAddr;
    string fName;
    string lName;
    string email;
    string className;
}

mapping (address => Student) Students;

address[] public studentAccts;

Setter Function:

 function setStudent (address _address, string _grade, string _fName, string _lName, string _email, string _className) public {
    var student = Students[_address];

    //Setting the variables of my Struct. 
    student.grade = _grade;
    student.studentAddr = _address;
    student.fName = _fName;
    student.lName = _lName;
    student.email = _email;
    student.className = _className;

    studentAccts.push(_address) -1;
}

Then my Getter

function getStudent(address _address) view public returns (string, address, string, string, string, string){
    return (Students[_address].grade, Students[_address].studentAddr, Students[_address].fName, Students[_address].lName, Students[_address].email, Students[_address].className);
}

Now on my the client side. My Function to populate the mapping for each address.

    Registrar.setStudent($("#address").val(), $("#grade").val(),$("#first_name").val(), $("#last_name").val(),$("#email").val(),$("#className").val(),(err, res) =>{
      console.log("this is my result", res);
      if(err){
        console.log(err)
      } 
    });

Then to retrieve the values in the mapping based on a specific address:

  $("#Retrieve").click(function(){
    const address = $("#address").val();

    Registrar.getStudent(address, (err, res) =>{
      console.log(res);

    })

  })

When I call the getStudent's function in my smart contract I am getting an array that looks like this: ["0x00000000000000000000000000000000", "0x0000000000000000000000000000000000000000", "0x00000000000000000000000000000000", "0x00000000000000000000000000000000", "0x00000000000000000000000000000000", "0x00000000000000000000000000000000"]

Not sure what I am missing here. Any help would be greatly appreciated!

  • 1
    The contract seems to be working correctly. Are you sure $("#address").val(); returns the correct address in your case? – Henk Aug 9 '18 at 7:16
  • 1
    @Henk You are correct - i forgot to specify TEXT as my input field so the function was being triggered incorrectly. Thanks for looking at my question. It should be all good now! A fresh set of eyes always helps when debugging – Pat Doyle Aug 9 '18 at 16:11
  • you should submit and accept your own answer to this problem, so others may learn as well – Shawn Tabrizi Aug 10 '18 at 7:23
0

I forgot to specify TEXT as my input field type so the function was being triggered incorrectly. Thanks for looking at my question. It should be all good now! A fresh set of eyes always helps when debugging

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.