0

I'm trying to pass the user's connected wallet into getNftsForOwner function but am not sure how to do it. If I type out the address into the function like this:

const NFTs = await alchemy.nft.getNftsForOwner('0x00000111');

Everything works just fine but if I try passing the connected wallet, it returns error 400: no wallet address.

import { useRouter } from "next/router";
import { alchemy } from "const/alchemy";
import { useEffect, useState } from "react";
import { OwnedNftsResponse } from "alchemy-sdk";
import { useAddress } from "@thirdweb-dev/react";



const BagGrabber: NextPage = () => {
    const router = useRouter();
    const { walletAddress } = router.query;
    const [NFTs, setNFTs] = useState<OwnedNftsResponse>()
    const address = useAddress(); //also tried passing this hook, maybe in useEffect?
  
 


 useEffect( () => {
    async function grabber() {
        const NFTs = await alchemy.nft.getNftsForOwner(walletAddress as string);
      setNFTs(NFTs);
    }
    grabber();
  }, [NFTs]);

Maybe I need to format the address in useEffect useState? I don't know? The owner address needs to be a string.

2
  • Hey. Nice first question. Just "like this", you pass the entire wallet as string, not the address. Shouldn't be something like "wallet.address" instead ? Apr 27, 2023 at 13:00
  • thanks for the response @Itération122442 After digging further I discovered that the issue is because the wallet address is undefined. I'll try setting it in useState but still fill like I'll run into trouble with syntax, ha... Argument of type 'string | string[] | undefined' is not assignable to parameter of type 'string'. Type 'undefined' is not assignable to type 'string'. Apr 27, 2023 at 15:34

1 Answer 1

0

Got it working only thing is that it continuously calls the api while page is actively being viewed. So that can get costly if not fixed. For some reason the wallet address has to be called owner for alchemy to accept it. If anyone knows how to fix that issue? please tell.

import { alchemy } from "const/alchemy";
import { useEffect, useState } from "react";
import { OwnedNftsResponse } from "alchemy-sdk";
import { useAddress } from "@thirdweb-dev/react";



const BagGrabber: NextPage = () => {
    const [NFTs, setNFTs] = useState<OwnedNftsResponse>()
    const owner = useAddress()



 useEffect( () => {
  if (owner) {
    async function grabber() {
        const NFTs = await alchemy.nft.getNftsForOwner(owner as unknown as string);
      setNFTs(NFTs);
    }
    console.log(NFTs);
    grabber();
  }  
  }, [NFTs, owner]);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.