我有一个在 sepolia 测试网上运行的 Solidity 合约,它只是注册和获取用户。显然,寄存器部分工作正常,但应该获取所有数据的 getAllUsers() 部分似乎返回空数组(预计将填充注册期间发送的数据)。下面是我的合同。 (部分部分除外)。
struct UserData {
string email;
string password;
bool deleted;
}
mapping(string => UserData) public users;
string[] public userIDs;
function getAllUsers() public view returns (string[] memory, UserData[] memory) {
UserData[] memory allUsers = new UserData[](userIDs.length);
for (uint i = 0; i < userIDs.length; i++) {
allUsers[i] = users[userIDs[i]];
}
return (userIDs, allUsers);
}
function register(string memory id, string memory email, string memory password) public returns (bool) {
if (bytes(users[id].email).length == 0 || bytes(users[id].password).length == 0){
return false;
}
UserData memory newUser = UserData(email, password, false);
users[id] = newUser;
userIDs.push(id);
return true;
}
getAllUsers().call()
的返回值是两个数组,但问题是它们是空的
{
"success": true,
"data": {
"0": [],
"1": [],
"__length__": 2
}
}
了解为什么它不返回数据吗?
尝试用天然气切换到
method.send()
。 Etherscan确实记录了交易,但没有返回数据
问题是,没有通过
register()
注册任何内容,如果您检查其返回值(真/假),由于检查无效,它将始终为假。
bool ok = monate.register("3", "[email protected]", "123456");
assert(ok == false);
看看这张无效支票
if (
bytes(users[id].email).length == 0 ||
bytes(users[id].password).length == 0
) {
return false;
}
我假设如果 id 已经注册,你打算返回 false,应该是这样的
if (
bytes(users[id].email).length != 0 ||
bytes(users[id].password).length != 0
) {
return false;
}