About NFT type alarm

Not sure what’s wrong with the parameter of getNFTs

import { useEffect, useState, useCallback } from "react";
import Moralis from "moralis";
import { Text, Link, Divider } from "@chakra-ui/react";

import { useNFTBalances, useMoralisWeb3Api } from "react-moralis";

type ChainType =
  | "eth"
  | "0x1"
  | "ropsten"
  | "0x3"
  | "rinkeby"
  | "0x4"
  | "goerli"
  | "0x5"
  | "kovan"
  | "0x2a"
  | "polygon"
  | "0x89"
  | "mumbai"
  | "0x13881"
  | "bsc"
  | "0x38"
  | "bsc testnet"
  | "0x61"
  | "avalanche"
  | "0xa86a"
  | "avalanche testnet"
  | "0xa869"
  | "fantom"
  | "0xfa";


type MyProps = {
  user: Moralis.User<Moralis.Attributes> | null;
  account: string;
};

export default function Nft({ user, account }: MyProps) {
  const chainId = Moralis.getChainId() as ChainType;
  
  const { getNFTBalances, data } = useNFTBalances();
  const Web3API = useMoralisWeb3Api();
  const [nfts, setNFTs] = useState([]);

  const fetchData = useCallback(async () => {
    await getNFTBalances({
      params: {
        chain: chainId,
        address: account,
      },
    });
  }, [account, chainId, getNFTBalances]);

  const getNFTs = async () => {
    console.log("get nfts run");
    let nfts = await Moralis.Web3API.account.getNFTs({ chain: chainId });

    if (nfts.result.length > 0) {
      nfts.result.forEach((n) => {
        console.log(JSON.parse(n.metadata));
      });
    }
  };
  getNFTs();

  useEffect(() => {
    setTimeout(() => {
      fetchData();
    }, 3000);
  }, [fetchData]);

  return (
    <Text fontSize="xl" fontWeight="bold">
      My NFTssss
    </Text>
  );
}

see somebody is using below code, but I don’t want to ignore check …

export async function getAllTokensForWallet(address: string): Promise<any[]> {
  const options = { chain: "mumbai", address };
  // @ts-ignore
  const resp = await Moralis.Web3API.account.getNFTs(options);
  return resp.result;
}

you have to specify the address youre fetching from

1 Like

Thanks.

const { getNFTBalances, data } = useNFTBalances();

and

let nfts = await Moralis.Web3API.account.getNFTs({ chain: chainId, address });

Which one is better? What’s the difference?

Depends if you want to use hooks from react-moralis or the base SDK Web3API method.

They are equivalent with the data that is returned but useNFTBalances has helpers you can use for React components e.g. displaying loading indicators.

1 Like