I have been bugged down for 2 days straight trying to figure out where I messed up but I can’t come to terms Here’s a full breakdown of the code I wrote
useEffect(() => {
const main = async () => {
const profileData = [];
try {
coll.map(async (col) => {
const instance = new Contract(col, nftABI, provider);
const balance = await instance.balanceOf(profile);
let profileBalance = balance.toNumber();
const tokenIds = [];
const name = [];
const images = [];
for (let i = 0; i < profileBalance; i++) {
if (profileBalance > 0) {
console.log(profileBalance, i, col);
const tokenId = await instance.tokenOfOwnerByIndex(profile, i);
let NumTokenId = tokenId.toNumber();
let StrTokenId = String(NumTokenId);
const uri = await instance.tokenURI(StrTokenId);
const url = uri.replace(
"ipfs://",
"https://gateway.pinata.cloud/ipfs/"
);
const ipfsFile = await fetch(url).then((r) => r.json());
const imageUri = ipfsFile.image.replace(
"ipfs://",
"https://gateway.pinata.cloud/ipfs/"
);
name.push(ipfsFile.name);
images.push(imageUri);
tokenIds.push(StrTokenId);
}
}
const obj = {
collection: col,
tokenArray: tokenIds,
image: images,
name: name,
};
if (obj.name.length > 0) {
profileData.push(obj);
}
});
} catch (error) {
console.log(error);
}
setR(profileData);
};
main();
}, []);
r, my useState variable comes back as an empty array and it console.logs first before the data has finished populating What am I doing wrong please