我有一些统一性代码,试图在其中收集ID(它们是存储在Struct上的值)。我有一个映射,键是地址,值是Structs数组。每当我执行getMediaByAddress
函数时,我都会得到一个invalid OpCode error
。任何帮助将不胜感激。
pragma solidity ^0.4.24;
contract MediaGallery {
address owner;
uint counter;
struct MediaAsset {
uint id;
string name;
address author;
uint createDate;
string[] tags;
string mediaHash;
}
mapping(address => MediaAsset[]) public mediaDatabase;
constructor () {
owner = msg.sender;
}
function addMedia(string _name, string _mediaHash) public returns (bool success) {
MediaAsset memory currentMedia;
currentMedia.id = counter;
currentMedia.name = _name;
currentMedia.author = msg.sender;
currentMedia.createDate = now;
currentMedia.mediaHash = _mediaHash;
mediaDatabase[msg.sender].push(currentMedia);
return true;
}
function addTag(uint _id, string _tag) public returns (bool success) {
mediaDatabase[msg.sender][_id].tags.push(_tag);
return true;
}
function getMediaByAddress(address _user) public view returns (uint[]) {
uint[] memory mediaAssetIds = new uint[](mediaDatabase[_user].length);
uint numberOfMediaAssets = 0;
for(uint i = 1; i <= mediaDatabase[_user].length; i++) {
mediaAssetIds[numberOfMediaAssets] = mediaDatabase[_user][i].id;
numberOfMediaAssets++;
}
return mediaAssetIds;
}
}
答案 0 :(得分:1)
您正在尝试读取数组末尾的内容。您的循环变数i
发生一次偏离错误。它的最大值是mediaDatabase[_user].length
,比数组末尾晚1。尝试以下方法:
for (uint i = 0; i < mediaDatabase[_user].length; i++) {