I have a smart contract that contains posts. Let's say that it's a twitter-like dapp.
Contract Blog {
struct Post {
string content;
bool isDeleted;
}
mapping (uint256 => Post) private posts;
uint256 postCounter = 0;
}
In this contract, I have a get method that returns post if post is not deleted.
function get(uint256 index) public view returns (string memory) {
Post memory post = posts[index];
required(post.isDeleted == false, "This post is deleted.")
return post.text
}
In this case, when I need to loop over Tweets I'm something like this on the frontend.
Get num of posts from the contract = X
for (let i=0; i<=X; i++) {
try {
post = try to get post from contract.
} except {
if contract returns error suppress it
continue
}
}
Most of the time the frontend side is trying to consume the contract for non-existing posts. Is there any better approach to do this?