So I was following the Clone Rarible tutorial and trying to customize the codes to my needs.
I am planning to add in the profile page a filter that will inform the user that the NFT that he owns is currently on sale right now.
The codes below is the solution I’ve got so far, but It’s not getting the result that I need.
Moralis.Cloud.define("getAllUserNFT", async (request) => {
const query = new Moralis.Query("EthNFTOwners");
query.equalTo("contract_type", "ERC721");
if(request.user.get("accounts")) {
query.containedIn("owner_of", request.user.get("accounts"));
const queryResults = await query.find();
const results = [];
for(let i = 0; i < queryResults.length; ++i){
const queryNFT = new Moralis.Query("NftAdded")
queryNFT.equalTo("token.owner_of", queryResults[i].attributes.owner_of);
queryNFT.equalTo("tokenId", queryResults[i].attributes.token_id);
queryNFT.equalTo("tokenAddress", queryResults[i].attributes.tokenAddress);
queryNFT.notEqualTo("isSold", true);
queryNFT.select("uid","askingPrice", "biddingTime", "createdAt", "tokenAddress","tokenId", "token.token_uri", "token.symbol","token.owner_of","token.id", "user.username");
const object = await queryNFT.first({useMasterKey: true});
logger.info("object",object);
if(object){
if (!object.attributes.token || !object.attributes.user) continue;
results.push({
"uid": object.attributes.uid,
"tokenId": object.attributes.tokenId,
"tokenAddress": object.attributes.tokenAddress,
"askingPrice": object.attributes.askingPrice,
"biddingTime": object.attributes.biddingTime,
"createdAt": object.attributes.createdAt,
"symbol": object.attributes.token.attributes.symbol,
"tokenUri": object.attributes.token.attributes.token_uri,
"ownerOf": object.attributes.token.attributes.owner_of,
"tokenObjectId": object.attributes.token.id,
"sellerUsername": object.attributes.user.attributes.username,
});
} else {
results.push({
"uid": '',
"tokenId": queryResults[i].attributes.token_id,
"tokenAddress": queryResults[i].attributes.token_address,
"askingPrice": '',
"biddingTime": '',
"createdAt": '',
"symbol": queryResults[i].attributes.symbol,
"tokenUri": queryResults[i].attributes.token_uri,
"ownerOf": '',
"tokenObjectId": queryResults[i].id,
"sellerUsername": ''
});
}
}
return results;
}
});