When an array of owners are returned (using the: getNFTOwners() code ), how do I pick the current NFT owner?

I run the following Moralis code in order to get the owner of a specific NFT:

const options = { address: “0xcbc67ea382f8a006d46eeeb7255876beb7d7f14d”, chain: “” };
const nftOwners = await Moralis.Web3API.token.getNFTOwners(options);
console.log(“nftOwners”, nftOwners.result)

What is confusing is that the above code yields, what seems to be an array of 500 objects/owners.
Being new to Moralis, I am not sure how to interpret this. Does the array of objects/owners suggest that this NFT has historically had 500 owners?
If so, am I right in assuming that to get the current owner, all I have to do is add to code:

console.log("CurrentNFT Owner: ", nftOwners.result[nftOwners.result.length - 1].owner_of): ?

I think that there are more than 500 results, it returns 500 results only on first page

and for every token_id can be an owner, that is why there could be lots of owners

1 Like

Oh wow…
I apologise in advance for the following questions, as I am still new to this crypto space.

Am I correct in assuming that all the addresses returned from the console.log(“nftOwners”, nftOwners.result.owner_of) for NFT token ( 0xcbc67ea382f8a006d46eeeb7255876beb7d7f14d) are ALL shareholders of the NFT in subject?

This now gets a bit more interesting, because I was in the process of developing a Dapp function with the purpose of validating if a user is the owner of the NFT or not before granting them access to certain functionalities only meant for the NFT owner!

there is an nft contract and multiple NFTs from that contract, nfts with different token_ids

Thanks for the prompt response…

So, if I understand you correctly, the address: 0xcbc67ea382f8a006d46eeeb7255876beb7d7f14d that I provided in my code below:

const options = { address: “0xcbc67ea382f8a006d46eeeb7255876beb7d7f14d”, chain: “” };

isn’t actually an NFT address but a contract address. And this contract address contains over 500 NFT’s?
Am I understanding this correctly?

maybe, it could also depend on what type of standard is that NFT contract ERC721 or ERC1155

They are of the ERC721 standard.

Am finding out that there is a lot more to NFT’s, contracts and ERC standards that I need to learn if I am going to realize my Dapp.

Take a look at the screen shot of result of code: console.log(“nftOwners”, nftOwners.result)

With reference to the image above, am I interpreting this correctly when I say:

The token_address holds the contract address and is not the NFT address?
I am confused to what the owner_of holds. Do these hold the addresses of the contract share holders?

Or how am I supposed to interpret this?

owner_of is the owner of that specific token_id from that specific token_address

1 Like

Aahh… I think I understand.

Is the value inside the owner_of equivalent to the user’s wallet address?

I ask because I am trying to write an NFT owner authentication function that, checks, whether the user is the owner of the NFT/token_id or not.

Something similar to the code below:

const options = { address: “0xcbc67ea382f8a006d46eeeb7255876beb7d7f14d”, chain: “” };
const nftOwners = await Moralis.Web3API.token.getNFTOwners(options);
var NFTs = nftOwners.result;

console.log("NFTs: ", NFTs)

const ifMyMetaMaskWalletsAddressWas = “0x903fac43e19ad7a2b59840c8d472de41f71a00fa”;
let counter = 0;
var NFT_Owner = false;


function myFunction() {
** //If both NFT address & Wallet address match then **
** //this is the owner of the NFT **
** if(NFTs[counter].owner_of == ifMyMetaMaskWalletsAddressWas)**
** {**
** console.log("You are the owner of this NFT as, "); **
** console.log("Your owner_of & your wallet Address both match: " +NFTs[counter].owner_of ); **
** NFT_Owner = true;**
** console.log("Your NFT name is: "+ NFTs[counter].name ); **
** console.log("Your NFT token ID is: " +NFTs[counter].token_id ); **
** }**

** counter++; **

yes, that should be the case

1 Like

I appreciate the prompt response.

Remember I am new in this space…
For development purposes, do you know where I can get free NFT tokens?
Are there any NFT Faucets?

Looking forward to your respose.

What nft tokens? You can deploy your own nft contract on testnet

1 Like

Oh I see…
Let me try this!

I noticed that console.log(NFTs[counter].token_uri) logs out the following link

When clicked, the link holds the value of the NFT object with a description, name and an image to the NFT.
I am interested in the image link. How do I access the image?

I tried console.log("NFT image: " +NFTs[counter].token_uri.image);
Which yields undefined.

You may have to parse that data as a json and then to extract the image

1 Like