[英]array value not showing length, but when i open so the element is exist
array value not showing length, but when i open so the element is exist.数组值不显示长度,但当我打开时元素存在。 but length and some other functions of array not working... """const getingLikedNft = async () => { setMyNftLoading(true); const res = await axios.get( ${BACKEND_URL}views_and_likes
); console.log("resshan", res); // let likedNft = []; var myLikedNft = new Array();但是长度和数组的一些其他功能不起作用...“”“const getingLikedNft = async () => { setMyNftLoading(true); const res = await axios.get( ${BACKEND_URL}views_and_likes
); console.log(" resshan", res); // 让 likedNft = []; var myLikedNft = new Array();
res?.data.forEach((element) => {
// console.log("xshan", element);
if (element?.likedAccounts.length > 0) {
element?.likedAccounts.forEach(async (elem) => {
if (elem?.toLowerCase() == user?.address?.toLowerCase()) {
console.log("resshan_elem", element);
try {
let res = await axios.post(`${BACKEND_URL}single-nft`, {
tokenId: element.tokenId,
tokenAddr: element.tokenAddr,
});
console.log("reslikedNFT", res.data);
// const uri = await contracts?.closedSeaNft?.methods
// .tokenURI(element.tokenId)
// .call();
// const res = await axios.get(uri);
var nftData = {
description: res?.data?.metadata?.description,
image: res?.data?.metadata?.imageUrl,
title: res?.data?.metadata?.name,
url: `/asset/${res?.data?.tokenAddr}/${res?.data?.tokenId}`,
};
console.log("reslikedN22FT2324232", Array.prototype.nftData);
myLikedNft = [...myLikedNft, nftData];
// myLikedNft.push({
// description: nftData.description,
// image: nftData.imageUrl,
// title: nftData.name,
// url: `/asset/${res?.data?.tokenAddr}/${res?.data?.tokenId}`,
// });
} catch (err) {
console.log("element?.likedAccounts [err]", err);
}
// likedNft.push(element);
// console.log("elem", elem);
}
});
}
});
console.log("reslikedNFT2321312341312dasd", myLikedNft);
setMyLikesCollection(myLikedNft);
setTimeout(() => {
setMyNftLoading(false);
}, 2000);
};""" };"""
you can see it is showing empty array.你可以看到它显示的是空数组。
but when i open this so the element is exists.但是当我打开它时,元素就存在了。
how can i solve this kindly help...我该如何解决这个好心的帮助...
You are making an async call for every iteration in the forEach
loop.您正在为forEach
循环中的每次迭代进行异步调用。 But the execution of the forEach
will finish before you get a response for each of your async calls.但是forEach
的执行将在您获得每个异步调用的响应之前完成。
When you print the myLikedNft
to the console, at the time of printing, the array has no data, because the async calls have not resolved.当您将myLikedNft
打印到控制台时,在打印时,数组没有数据,因为异步调用尚未解析。 But when you expand them in the console, it gives you the latest value of the expression.但是当您在控制台中展开它们时,它会为您提供表达式的最新值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.