I have written a basic contract like this to store certificates:
pragma solidity ^0.4.2;
/// @title Sending and storing certifications to the blockchain
contract CertificationStore {
struct Certification {
string firstName;
string lastName;
string certName;
string institution;
string description;
uint year;
uint month;
uint day;
}
mapping (address => Certification) public certifications;
Certification[] public certArray;
address[] public addressIdx;
uint numCerts;
Certification public aCert;
event Send(address indexed _from);
function CertificationStore() {
}
function getCertArrayLength() returns(uint) {
return certArray.length;
}
function getAddressIdxLength() returns(uint) {
return addressIdx.length;
}
function sendCert(address acct, string fn, string ln, string cn,
string ins, string desc, uint yy, uint mm, uint dd) returns(uint certId) {
certifications[acct] = Certification({
firstName: fn,
lastName: ln,
institution: ins,
description: desc,
certName: cn,
year: yy,
month: mm,
day: dd
});
certId = numCerts++;
addressIdx[certId] = acct;
certArray[certId] = Certification({
firstName: fn,
lastName: ln,
institution: ins,
description: desc,
certName: cn,
year: yy,
month: mm,
day: dd
});
Send(acct);
}
function getFirstName(address _from) public returns(string) {
for (uint i = 0; i < certArray.length; i++) {
Certification cert = certArray[i];
string name = cert.firstName;
address acct = addressIdx[i];
if(acct == _from) {
return name;
}
}
return ("no name found");
}
}
I am using web3 and truffle to create a frontend to interact with this contract.
On the frontend, when I call sendCert with all the values, it seems to add them to the map but NOT to the array. I tried adding to the array the way shown above and with the .push() method and when I call the two functions to check the certArray length and the addressIdx array length, it ALWAYS returns 0.
The method sendCert, which is supposed to add the certifications to the array and the mapping also returns 0 for that certId no matter how many times I add certifications.
Am I doing something wrong here? In the frontend, I have a call back which logs "success" when the certification is sent successfully and executed by this method successfully.
Lastly, is there any way I can send the contents of the two arrays to the frontend to work on them in javascript instead of solidity?
Thank you for your help and patience!