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!
$("#address").val();
returns the correct address in your case?