在我的智能契约中,我有一个struct类型的NotaryEntry和一个函数addNotaryEntry,它创建了一个结构实例并添加到一个映射中,我确实将正确的值传递给函数,但是这个结构是用零值创建的,并且忽略了我解析的值,即使我将正确的参数类型值传递给了该函数。我如何解决这个问题?谢谢
这就是addNotaryEntry创建的函数:
1: string: description
2: uint256: timestamp 0
3: string: hash
4: address: setBy 0x0000000000000000000000000000000000000000
5: bool: isSet false下面是我的代码:
uint filesCount;
mapping(uint => NotaryEntry) public files;
mapping(address => uint[]) public personFiles;
struct NotaryEntry{
uint id;
string description;
uint timestamp;
string hash;
address setBy;
bool isSet;
}
function addNotaryEntry( string memory _hash, string memory _desc) public {
uint _id = filesCount += 1;
NotaryEntry memory notary;
notary.id = _id;
notary.description = _desc;
notary.timestamp = now;
notary.hash = _hash;
notary.setBy = msg.sender;
notary.isSet = true;
personFiles[msg.sender].push(filesCount);
}
`发布于 2019-07-23 21:35:31
您可以使用存储指针将存储指针写入存储,而不是创建在函数完成后将被丢弃的内存变量。
改变这一点:
NotaryEntry记忆公证;
对此:
NotaryEntry storage notary = files[filesCount];
在这里看一看,了解为什么会起作用:https://blog.b9lab.com/storage-pointers-in-solidity-7dcfaa536089
这可能也值得一看:是否有妥善解决的和简单的存储模式的坚固性?
希望能帮上忙。
发布于 2019-07-23 21:20:45
问题是,您没有将数据写入存储。
这一行:
NotaryEntry memory notary;
创建一个新的NotaryEntry并将其保存在内存中。在函数结束时,它将被忽略,因此您对它所做的任何更改都将丢失。
相反,您应该编写NotaryEntry映射的属性,即:
files[_id].id = _id;
files[_id].description = _desc;
files[_id].timestamp = now;
///...etc发布于 2019-07-23 21:25:03
如果不看到代码的其余部分,很难判断,但是基于这个函数,看起来您正在内存中生成新的NotaryEntry,并且永远不会将它保存到持久的数据结构中。尝试在files.push(notary)底部添加类似addNotaryEntry的内容。那么,下面的人应该表现得很好:
function getNotaryEntry(uint int) public view {
return files[id]
}它看起来像是在保存一个空对象,因为当您在实体中定义一个结构数组时,空数组的位置看起来就像一个空的结构。如果没有错误或空值,就会得到一个形状正确的结构,其中每个值本身都是空的。
对我来说超级怪异的行为,花了一段时间才适应。
https://ethereum.stackexchange.com/questions/73231
复制相似问题