How to use an array of address to fetch multiple data from moralis

hey guys i have been attempting to fetch data from moralis using an array but i doesnt work, it works with one address but when i try multiple it doesnt here is my code

export const creator = (addr) => {

    const params = { ethAddress: addr.map((i) => (i)).toString() }

    const { data } = useMoralisCloudFunction("getOwnerInfo", params);

 console.log(params)

    return data

};

here is my cloud code

Parse.Cloud.define("getOwnerInfo", async ({ params }: any) => {

  const query = new Parse.Query("_User");

  query.equalTo("ethAddress", params.ethAddress)

  const results = await query.find({useMasterKey:true});

  return results;

});

You can try this and use addresses instead of ids

thanks but where does that code go does it go to the cloud function or front end

You can create a cloud function bases on that code

thanks that worked i have one last question tho, here i have this array

const featured = ['0x8df29e0c1217344dcca3266e8062ea189a10250a', '0x357f77a02be5a4a80864297c34aa2f6ef3fdb59a']

 const data = await contract.fetchHisNFTs(featured);

do you have any idea how i can pass the address in the fetch nft function 1 by 1

what is that function? how does it work?

here is the full code

const featured = ['0x8df29e0c1217344dcca3266e8062ea189a10250a', '0x357f77a02be5a4a80864297c34aa2f6ef3fdb59a']

    useEffect(() => {
        LoadUserNFT(featured , true)
            .then((items) => {
                const Items = items.filter(i => i.filetype !== "video/mp4" && i.filetype !== "video/quicktime")
                setNfts(Items.slice(0, 5));
                setLoadingState(false)
            });
    }, []);


const LoadUserNFT = async (userId, polygon) => {

        const provider = new ethers.providers.JsonRpcProvider(polygon && process.env.NEXT_PUBLIC_ALL_SECRETPOLY || !polygon && process.env.NEXT_PUBLIC_ALL_SECRET)

        const contract = !polygon

            ? fetchContract(provider)

            : fetchContractPoly(provider)

        const data = await contract.fetchHisNFTs(userId);

        const items = await Promise.all(

          data.map(async (i) => {

            const tokenUri = await contract.tokenURI(i.tokenId);

            const meta = await axios.get(tokenUri);

            let owner = await contract.ownerOf(i.tokenId.toNumber())

            let price = ethers.utils.formatUnits(i.price.toString(), "ether");

            let maxbid = ethers.utils.formatUnits(i.maxBid.toString(), "ether");

          let item = {

            price,

            maxbid,

            listed: i.listed,

            maxBidder: i.maxBidder.toLowerCase(),

            bid: i.bid,

            tokenId: i.tokenId.toNumber(),

            seller: i.seller.toLowerCase(),

            owner,

            expiration: i.expiration.toString(),

            sold: i.sold,

            poster: meta.data.poster?.toString(),

            filetype: meta.data.filetype?.toString(),

            image: meta.data.image,

            name: meta.data.name,

            description: meta.data.description,

          };

            return item;

          })

        );

        return items

      }

so what the code does is fetch individual nft of every address i pass in

this does not work it returns both addresses

const data = await contract.fetchHisNFTs(userId.map((i) => (i)));

it gives this error

Error: invalid address or ENS name (argument="name", value=["0x8df29e0c1217344dcca3266e8062ea189a10250a","0x357f77a02be5a4a80864297c34aa2f6ef3fdb59a"], code=INVALID_ARGUMENT, version=contracts/5.7.0)

what is the abi of this function? does it accept arrays or only individual addresses?

It accepts individual addresses

In this case, you will have to loop over your array and call that function for every address

Can you please give me an example please

for those of you wondering i fixed it already here is my code

const LoadAllUserNFT = async (userIds, polygon) => {
        const provider = new ethers.providers.JsonRpcProvider(polygon ? process.env.NEXT_PUBLIC_ALL_SECRETPOLY : process.env.NEXT_PUBLIC_ALL_SECRET);
        const contract = polygon ? fetchContractPoly(provider) : fetchContract(provider);

        const itemsArrays = await Promise.all(
            userIds.map(async (userId) => {
                const datas = await contract.fetchHisNFTs(userId);
                const data = datas.slice(0, 5);
                const items = await Promise.all(
                    data.map(async (i) => {
                        const tokenUri = await contract.tokenURI(i.tokenId);
                        const meta = await axios.get(tokenUri);

                        const price = ethers.utils.formatUnits(i.price, "ether");
                        const maxBid = ethers.utils.formatUnits(i.maxBid.toString(), "ether");
                        const owner = await contract.ownerOf(i.tokenId.toNumber());

                        const item = {
                            price,
                            maxBid,
                            listed: i.listed,
                            maxBidder: i.maxBidder.toLowerCase(),
                            bid: i.bid,
                            tokenId: i.tokenId.toNumber(),
                            seller: i.seller.toLowerCase(),
                            owner,
                            expiration: i.expiration.toString(),
                            sold: i.sold,
                            poster: meta.data.poster?.toString(),
                            filetype: meta.data.filetype?.toString(),
                            image: meta.data.image,
                            name: meta.data.name,
                            description: meta.data.description,
                            polygon,
                        };

                        return item;
                    })
                );
                return items;
            })
        );

        return itemsArrays.flat();
    };
1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.