I've got the following contract:
pragma solidity ^0.4.11;
contract Zusatzaufgabe {
struct Student {
string name;
string vorname;
uint matnr;
uint punkte;
bool[] solved;
}
address[] public addressen;
mapping(address => Student) public studenten;
modifier hurdle(uint _n) {
require(_n == msg.value/2);
_;
}
modifier onlyTillTest() {
require(block.timestamp < 1499702400);
_;
}
function Zusatzaufgabe(){
}
function register(string _name, string _vorname, uint _matnr) onlyTillTest(){
studenten[msg.sender] = Student(_name,_vorname,_matnr,1,new bool[](3));
addressen.push(msg.sender);
}
function onepoint() payable hurdle(10000) onlyTillTest(){
if(studenten[msg.sender].solved[0] == false){
studenten[msg.sender].solved[0] = true;
studenten[msg.sender].punkte += 1;
}
}
function twopoints(uint _value) onlyTillTest(){
if(studenten[msg.sender].solved[1] == false
&& _value == studenten[addressen[0]].matnr
){
studenten[msg.sender].solved[1] = true;
studenten[msg.sender].punkte += 2;
}
}
function sixpoints(bytes32 _h, uint8 _v, bytes32 _r, bytes32 _s) onlyTillTest(){
if(
studenten[msg.sender].solved[2] == false
&& ecrecover(_h, _v, _r, _s) == msg.sender
&& _h == sha3(this)
){
studenten[msg.sender].solved[2] = true;
studenten[msg.sender].punkte += 6;
}
}
}
To sucessfully use the function twopoints
, I need to get the value of studenten[addressen[0]].matnr
.
How can I access this by console with web3?
MyContract.studenten[addressen[0]].matnr
doesn't work, even though it is public.
ReferenceError: addressen is not defined
How do I access this value?
Best Answer
This unit test works for me with the above contract using node 8.1.2 and truffle 3.4.3.
The last call will return a tuple with the fields from struct Student. It will return [name, vorname, matnr, punkte]. The field solved is not returned, see https://solidity.readthedocs.io/en/develop/contracts.html#getter-functions for the documentation about getters automatically generated. Also int fieds (matnr and punkte) are returned as bignums.