How to get the NFT and check if they are on Sale on the Marketplace right now?

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;      
    }              
});

Hey @appsofdave

Do you see logger.info("object",object) in the console?
I suggest you to make a small change: logger.info("object",JSON.stringify(object))

Pelase share your server subdomain :mage:

maybe you also want to use MasterKey for first find too: const queryResults = await query.find();

Here’s my subdomain: 2ag3bcl3ickr.bigmoralis.com

The object is empty, that’s the issue that I have.

I will try. Thanks! I added it before, I dunno why it’s not there haha, I think I had an error or something that’s why I removed it. But will update you on the output.

I see that there is information in the console.

The problem is that to show the data in your logger you need to stringify objects

The object is empty, I added the stringify already.

If you remove some of these filters it works fine?

   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);

I just tried that and turns out it’s empty. Hmmmm

Could you share the command you use to call this cloud function?

What I can see in your console is that you tried to call it with only provided wallet address.
But you have request.user.get("accounts") which requires you to call this cloud function from the frontend when the suer is logged in