简体   繁体   English

TypeError:在设置 MetaMask 以显示 NFT 时无法读取未定义的属性(读取“地图”)

[英]TypeError: Cannot read properties of undefined (reading 'map') while setting up MetaMask to display NFT

I keep getting this error when I run my code Uncaught TypeError: Cannot read properties of undefined (reading 'map') I am tring to set up a Metamask which displays the users NFTS that they have purchased from OpenSea when they connect their metamask account I'll show my code to show what I have done and if anyone knows how to fix this could they post a solution code as this would be of so much help.我在运行代码时不断收到此错误 Uncaught TypeError: Cannot read properties of undefined (reading 'map') '将显示我的代码以显示我所做的事情,如果有人知道如何解决这个问题,他们可以发布解决方案代码,因为这会有很大帮助。

import { useEffect, useState } from 'react';
import './nft.css'
import NFTContainer from './NFTContainer'

export function Nft() {

    const [walletAddress, setWalletAddress] = useState(null)
    const [nfts, setNfts] = useState()

    const connectWallet = async () => {
        if (typeof window.ethereum !== 'undefined') {

            const accounts = await window.ethereum.request({ method: 'eth_requestAccounts' });

            setWalletAddress(accounts[0])
        }
    }

    const getNftData = async () => {

        if (!walletAddress) return;

        const response = await fetch(`https://api.rarible.org/v0.1/items/byOwner/?owner=ETHEREUM:${walletAddress}`)

        const data = await response.json()

        debugger

        setNfts(data.items)
    }

    useEffect(() => {
        getNftData()
    }, [walletAddress])


    return (
        <div className='Nft'>
            <div className='text'>
                Account: {walletAddress}
            </div>
            <button className='connect-button' onClick={connectWallet}>
                Connect Wallet
            </button>
            <NFTContainer nfts={nfts} />
        </div>
    );
}
export default Nft;

import React from 'react'
import NFTCard from './NFTCard'

const NFTContainer = ({ nfts }) => {

    return (
        <div>
            {nfts.map((nft, index) => {
                return <NFTCard nft={nft} key={index} />
            })}
        </div>
    )
}

export default NFTContainer

So when I put in the nft.meta.name I keep getting the uncaught type error and wondering as to why this error keeps appearing因此,当我输入 nft.meta.name 时,我不断收到未捕获的类型错误,并想知道为什么这个错误不断出现

import React from 'react'

const NFTCard = ({ nft }) => {

    return (
        <div>
            {nft.meta.name}
        </div>
    )
}

export default NFTCard

You are missing the initial value here,您在这里缺少初始值,

const [nfts, setNfts] = useState([]);

You must use the default value while using the useState() hook.使用useState()挂钩时必须使用default值。 If you want to apply array.map() method on state value then have to declare hook with empty array useState([]) .如果你想对状态值应用array.map()方法,那么必须用空数组useState([])声明钩子。

the problem is you defined your useState like this问题是你这样定义你的useState

const [nfts, setNfts] = useState()

So if you don't define any value to your state then by default it is undefined and you can't map through undefined value, so define your state like this因此,如果您没有为您的状态定义任何值,那么默认情况下它是undefined的,并且您无法映射undefined的值,因此像这样定义您的状态

import { useEffect, useState } from 'react';
import './nft.css';
import NFTContainer from './NFTContainer';

export function Nft() {
  const [walletAddress, setWalletAddress] = useState(null);
  const [nfts, setNfts] = useState([]);

  const connectWallet = async () => {
    try {
      if (typeof window.ethereum !== 'undefined') {
        const accounts = await window.ethereum.request({ method: 'eth_requestAccounts' });
        setWalletAddress(accounts[0]);
      }
    } catch (error) {
      console.log('err1==>', error);
    }
  };

  const getNftData = async () => {
    try {
      if (!walletAddress) return;
      const response = await fetch(`https://api.rarible.org/v0.1/items/byOwner/?owner=ETHEREUM:${walletAddress}`);
      const data = await response.json();
      setNfts(data.items);
    } catch (error) {
      console.log('err2==>', error);
    }
  };

  useEffect(() => {
    getNftData();
  }, [walletAddress]);

  return (
    <div className='Nft'>
      <div className='text'>Account: {walletAddress}</div>
      <button className='connect-button' onClick={connectWallet}>
        {!walletAddress ? 'Connect Wallet' : 'Wallet Connected'}
      </button>
      <NFTContainer nfts={nfts} />
    </div>
  );
}
export default Nft;

Note: Also do error handling and show loader when API is fetching data from network or from chain注意:当 API 从网络或链中获取数据时,还要进行错误处理并显示加载器

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TypeError:无法读取未定义的属性(读取“bufferToHex”)-元掩码 - TypeError: Cannot read properties of undefined (reading 'bufferToHex') - Metamask TypeError:无法读取未定义的属性(读取“地图”) - State 返回未定义 - TypeError: Cannot read properties of undefined (reading 'map') - State returns undefined TypeError:无法读取未定义的属性(正在读取“映射”)(JavaScript 数组映射) - TypeError: Cannot read properties of undefined (reading 'map') (JavaScript array map) 为输入网络元素设置属性时出现错误“TypeError:无法读取未定义的属性(读取“setAttribute”)” - Getting an error 'TypeError: Cannot read properties of undefined (reading 'setAttribute')' while setting an attribute for the input webelement 无法显示数组项和标志错误:未捕获的类型错误:无法读取未定义的属性(读取“地图”) - failed to display array items and flags error: Uncaught TypeError: Cannot read properties of undefined (reading 'map') TypeError:无法在 React-Views 中读取未定义的属性(读取“地图”) - TypeError: Cannot read properties of undefined (reading 'map') in React-Views TypeError:无法在反应 JS 中读取未定义的属性(读取“地图”) - TypeError: Cannot read properties of undefined (reading 'map') in react JS React-Redux - 类型错误:无法读取未定义的属性(读取“地图”) - React-Redux - TypeError: Cannot read properties of undefined (reading 'map') 类型错误:无法读取未定义的属性(读取“地图”)Table.render - TypeError: Cannot read properties of undefined (reading 'map') Table.render 反应:类型错误:无法读取未定义的属性(读取“地图”) - React: TypeError: Cannot read properties of undefined (reading 'map')
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM