Get nft count of loged in user

I have just made the authentication work in NextJS.

Next I tried this to get the NFT count.
const nftCount = await Moralis.Web3.getNFTsCount();

But maybe because that I am a newbie I couldn’t successfully do it.

Can you please provide me a sample script in NextJS to try this.

Here is my login script inside navbar component.

// import Image from 'next/image'
import styles from '../styles/Navbar.module.css'

import { useMoralis } from "react-moralis";

const Navbar = () => {

  const { authenticate, isAuthenticated, logout, user } = useMoralis();

    return (
        <div className={styles.container}>
            <div className="container-fluid">
                <div className="row">
                    <div className="col-6">
                        <img src="/nft-nextjs/img/logo.png" alt="" width="210px" height="94px" />
                    <div className="col-6 ">
                        <div id="login-header-icons">
                          {isAuthenticated ? (
                                <button onClick={logout}>Logout</button>
                          ) : (
                              onClick={() => {
                                authenticate({ provider: "metamask" });
                              Sign in with MetaMask

function maskEthAddress(eth_address) {
    return eth_address.substring(0,4) + "..." + eth_address.slice(-4);

export default Navbar

I don’t know if this API endpoint works well now, this is a deprecated syntax with Moralis.Web3

it looks like it worked when I tested it now, it required an address when I wasn’t authenticated:

nftCount = await Moralis.Web3.getNFTsCount({address: "ADDRESS"})

what is the new syntax to get nft count?
Can you please show me an example. Should it be in the getStaticProps function?

Does this import is enough

import { useMoralis } from “react-moralis”;

And inside the component

const { Moralis } = useMoralis();

there isn’t a new syntax yet to get the NFTs count from what I know, you can use a work around by listing the nfts for a wallet and getting from there the number of nfts (there is a count field on first page, and you can use limit 1)

This is what i did with plain javascript first. Now i started to migrate it to NextJS.

async function getNFTCount() {
  // get polygon NFTs for address
  const options = { chain: 'eth', address: loged_user_eth_addresss };
  var nfts = await Moralis.Web3API.account.getNFTs(options);
  nfts = nfts.result;

  eth_address = user.get('ethAddress');
  eth_address_mask = eth_address.substring(0,4) + "..." + eth_address.slice(-4);

  // calculate total venom points
  var venom_points = 0;
  nfts.forEach(function (nft) {
    venom_points += getVenomPoints(nft.synced_at);
  venom_points = Math.round(venom_points);

  // display holdings component
    <table border="1" class="text-white">
        <tr align="center">
            <td><h3>Welcome</h3> <br/> ${ eth_address_mask } <br/> [${ user.get('ethAddress') }] </td>
            <td><h3>Venom Points</h3> <br/> ${ venom_points } </td>
        <tr align="center">
            <td><h3>Hornets Held</h3> <br/> ${ nfts.length } </td>
            <td><h3>RAWR Balance</h3> <br/> N/A</td>

}//endof getNFTs()


import { useMoralis } from "react-moralis";

const TestNFTProject = () => {

  const { Moralis } = useMoralis();

  async function getNFTCount() {
    return await Moralis.Web3.getNFTsCount({address: "0x76299b8be5ba5723cf4c60fc41c76df30e094922"});

  const nftCount = getNFTCount();


  return (
      {error && <>{JSON.stringify(error)}</>}

export default TestNFTProject

This is how I got NFT count of loged in user. Is this correct?

  const options = { chain: 'eth', address: site_owner_eth_address };

  async function getNFTCount(options) {
    const transfersNFT = await Moralis.Web3API.account.getNFTTransfers(options);
    const nft_transfers = transfersNFT.result;

    const nfts = nft_transfers.filter(nft_transfer => nft_transfer.to_address == loged_user_eth_addresss);

  }//endof getNFTs()

  useEffect(() => {
    if (isInitialized) {
  }, [isInitialized]);

It looks like you look on transfers and not on nfts in particular

Ya, in one page I just need the nft count of loged in user.

In another page i want nft image, name and price.

Actually my client sold some NFTs in opensea. He want to show those details in his website.
He want to show the details of NFTs which he sold to the loged in user.

But i am not getting image, price and name in the way i got it using plain javascript.

Here is the code i used in NextJs.

  useEffect(() => {    
    let isMounted = true;     
    async function getNFTs(options) {
        // get NFTs for address
        const nft_response = await Moralis.Web3API.account.getNFTs(options);
        const nfts = nft_response.result;

          nfts.forEach(function (nft) {
            if (nft.token_uri) {
              let url = fixURL(nft.token_uri);

               .then(response => response.json())
               .then(data => {
          })//endof forEach()

        if (isMounted) setNfts(nfts);

    }//endof getNFTs()

    if (isInitialized) {
    return () => { isMounted = false };
  }, [isInitialized]);

you could add some logging here to see what data is there

error showing is

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at (Reason: CORS header ‘Access-Control-Allow-Origin’ missing).

url is logging successfully.

          nfts.forEach(function (nft) {
            if (nft.token_uri) {
              let url = fixURL(nft.token_uri);

          })//endof forEach()

ok, you could try to fetch the url with a cloud function, you can also try to see if the information is not already present in metadata field for that nft info

what is the alternative now instead of instead Moralis.getNFTsCount() ?

You can use getNFTs and you will have the total in the first result

oh, how much extra information will be returned there

You can use limit of 1 to return o my the information for 1 nft