javascript 无法解决未定义(阅读“长度”)错误

yvt65v4c  于 2023-03-28  发布在  Java
关注(0)|答案(1)|浏览(126)

我正在构建一个NFT markeplace项目,当我试图获取创建的NFT时,我得到了这个错误:enter image description here .
我得到错误的页面的代码是这样的:

import React, {useEffect, useState, useContext} from "react";
import Style from "../CosmetizarePagini/paginatoateNFTurile.module.css";
import {Incarca} from "../module/indexmodule";
import ImagineCreazaNFT from "../ImagineCreazaNFT/ImagineCreazaNFT";
import {NFTMarketplaceContext} from "../Context/NFTMarketplaceContext";

const ToateNFTurile = () => 
{
  const {fetchNFTs, currentAccount} = useContext (NFTMarketplaceContext);
  const [nfts, setNfts] = useState ([]);
  const [nftsCopy, setNftsCopy] = useState ([]);

  useEffect(() => 
  {
    try 
    {
      if (currentAccount) 
      {
      fetchNFTs().then((items) => 
      {
        setNfts(items);
        setNftsCopy(items);
      });
      }
    } 
    catch (error) {console.log ("Reincarca browser-ul, a aparut o eroare!", error);}
  }, []);

  return (
    <div className={Style.paginatoateNFTurile}>
      <h1><center>Colectie NFT-uri</center></h1>
      <h2><center>Click pe imagini pentru detalii!</center></h2>
      <br></br><br></br><br></br>
      
      {nfts.length == 0 ? <Incarca/> : <ImagineCreazaNFT NFTData={nfts}/>}
    </div>
  );
};

export default ToateNFTurile;

我想提一下,我是一个初学者程序员,我(不成功:))试图解决它,但无济于事。任何帮助是赞赏。
谢谢!
尝试修改上下文文件中的代码,但出现相同错误。

njthzxwz

njthzxwz1#

您可以console.log()items变量进行检查,可能是它不是一个正确的数组或null或未定义.因为它不是正确的数组,你得到这个错误后,API响应.

fetchNFTs().then((items) => 
      {
        // Add a console.log here
        console.log(items)
        setNfts(items);
        setNftsCopy(items);
      });

相关问题