as part of learning solidity and smart-contract development, I am developing a simple part management/tracking system. I have a struct and a mapping for a list of parts as such:
struct Part {
uint pNumber;
string pName;
string pDesc;
string pManuf;
string[] owners;
}
mapping(uint => Part) public part_store;
uint public partsCount;
I'm adding new parts to the mapping using the following method:
function createPart (string _pName, string _pDesc, string _pManuf) public {
partsCount++;
part_store[partsCount].pNumber = partsCount;
part_store[partsCount].pName = _pName;
part_store[partsCount].pDesc = _pDesc;
part_store[partsCount].pManuf = _pManuf;
part_store[partsCount].owners.push(_pManuf);
/* Trigger event */
emit partCreatedEvent(partsCount);
}
Everything is compiling without errors and I'm able to submit this transaction through Truffle. I get a transaction ID when I create new parts.
Now I'm trying to read the data inside of part but I don't see the array where I pushed the data. It seems that the last data in the object is the manufacturer. But it doesn't show that I've pushed the original manufacturer as owner of that part.
> contract.part_store(1)
Result:
[ BigNumber { s: 1, e: 0, c: [1] },
'part name',
'part desc',
'part Manuf' ]
>
Have I not properly defined the array and pushed data to it? How can I read the data inside of that array? I need an array because this is an easy way to store owners for the parts since owners can change (i.e. defining a new owner by pushing new owner to the array for the specific part).
Thanks all.
EDIT: It seems that if I define an array outside of a struct and public
, I am able to read the value:
string[] public test1;
string[] test2;
function writeElem() public {
test1.push("t1");
test2.push("t2");
}
results:
> contract.writeElem()
...tx: 0x88...
...txHas: 0x88...
> contract.test1(0)
't1'
> contract.test2(0)
TypeError: app.test2 is not a function
> contract.test2
undefined
I don't think I can define variables in struct public or private so maybe that's why I'm unable to read that data? Could it be something with data access for arrays in Solidity?