简体   繁体   English

数组值不显示长度,但当我打开时元素存在

[英]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.

相关问题 当特定值存在时如何过滤JSON数组的一个元素 - How to filter one element of JSON array when specific value exist in it 我要存储数组的长度,但是当单击提交按钮时,我使用array.splice,所以数组的长度会下降吗? - I am to store the length of an array but when the submit button is clicked I use array.splice so the length of the array goes down? 当数组作为参数传递并且长度还不存在时,如何利用函数中的数组长度(在JavaScript中)? - How do I utilize the length of an array in a function (in JavaScript) when the array is being passed as a parameter and the length does not exist yet? Google Map Infowindow无法正常显示,但是当我打开firbug时,它运行良好 - Google Map Infowindow not showing properly but when i open firbug so it's working well 填充时显示0长度的Javascript数组 - Javascript array showing 0 length when populated 检查对象值数组是否在另一个数组中但长度不同 - Check if array of object value exist in another array but different length shift更改数组的长度,因此不会执行最后一个元素 - shift changes the length of the array so the last element is not going to be executed ,我需要修改 anArray 以便将数组中具有 oldVal 值的所有元素替换为 newVal 的值 - , I need to modify anArray so that all element in the array that have the value of oldVal get replaced with the value of newVal 当数组尚不存在时,如何在 Redux 中的 reducer 中向数组添加元素? - How do I add an element to array in reducer in Redux when the array does not yet exist? Mootools:当element / div不存在时,如何停止显示并出错 - Mootools: How to stop showing and error when the element/div dont exist
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM