从坚固性中获取所有项目
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";
import "@openzeppelin/contracts/utils/Counters.sol";
import "@openzeppelin/contracts/utils/Strings.sol";
contract DgramMap is ERC721URIStorage {
struct TokenDetails {
string src;
uint256 sell_price;
string uniqueId;
}
mapping(address => mapping(uint256 => TokenDetails)) public userStructs;
constructor(address payable _admin) ERC721("Dgram Token", "DT") {
}
function add(uint256 _id, address _user) public returns (uint256) {
TokenDetails memory _tokenDetails = TokenDetails(
"abc.com",
12,
"abcs1234"
);
userStructs[_user][_id] = _tokenDetails;
return _id;
}
function deleteMap(uint256 _tokenId, address _user) public returns (uint256) {
delete userStructs[_user][_tokenId];
return _tokenId;
}
function getItems(address _u) public view returns (TokenDetails memory) {
// Here I want to return hall TokenDetails[_u]
return TokenDetails[_u];
}
}
我如何从getItems获得tokendetails [_U]大厅?我在这里也无法使用长度。如果有的话,建议任何替代方法。 我想要这种类型的结构[{keyname:{}}]。
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";
import "@openzeppelin/contracts/utils/Counters.sol";
import "@openzeppelin/contracts/utils/Strings.sol";
contract DgramMap is ERC721URIStorage {
struct TokenDetails {
string src;
uint256 sell_price;
string uniqueId;
}
mapping(address => mapping(uint256 => TokenDetails)) public userStructs;
constructor(address payable _admin) ERC721("Dgram Token", "DT") {
}
function add(uint256 _id, address _user) public returns (uint256) {
TokenDetails memory _tokenDetails = TokenDetails(
"abc.com",
12,
"abcs1234"
);
userStructs[_user][_id] = _tokenDetails;
return _id;
}
function deleteMap(uint256 _tokenId, address _user) public returns (uint256) {
delete userStructs[_user][_tokenId];
return _tokenId;
}
function getItems(address _u) public view returns (TokenDetails memory) {
// Here I want to return hall TokenDetails[_u]
return TokenDetails[_u];
}
}
How can I get TokenDetails[_u] hall from GetItems?? I am not able to use length also here. Suggest any alternative way if any.
I want this type of structure [ { keyname: {} }].
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以在映射中嵌套存储数组,而不是嵌套映射,如下所示:
这样,您可以通过将每个项目分别返回
groupID
和itemid Idid
getItem(...)
,仍然能够通过将groupId
传递到getItems(...)。与映射不同,这是阵列的作用。
Instead of a nested mapping, you could nest a storage array inside a mapping as follows:
That way, you could return each item individually by passing in a
groupId
and aitemId
togetItem(...)
, and still be able to return an entire nested storage array just by passing in agroupId
togetItems(...)
. This works as arrays are iterable, unlike mappings.