简体   繁体   English

在运行使用solidity 智能合约的next.js 应用程序时,出现“无法读取未定义的属性”错误

[英]While running my next.js app that is using solidity smart contracts, I am getting "Cannot read properties of undefined" error

I was running my next.js app and trying to fetch user I am getting "cannot read properties of undefined" error我正在运行我的 next.js 应用程序并尝试获取用户我收到“无法读取未定义的属性”错误

在此处输入图像描述

And following error in the console在控制台中出现以下错误在此处输入图像描述

Below is the code I was using下面是我使用的代码

import Ewitter from './Ewitter.json';
import ethers from 'ethers';
import { useState, useEffect } from 'react';
const ContractABI = Ewitter.abi;
const ContractAddress = '0x5FbDB2315678afecb367f032d93F642f64180aa3';
const Ethereum = typeof window !== 'undefined' && (window as any).ethereum;

const getEwitterContract = () => {
  const provider = new ethers.providers.Web3Provider(Ethereum);
  const signer = provider.getSigner();
  const EwitterContract = new ethers.Contract(
    ContractAddress,
    ContractABI,
    signer
  );

  return EwitterContract;
};

const useEwitter = () => {
  // const Ewitter = getEwitterContract();

  const [currentAccount, setCurrentAccount] = useState<string>('');
  const [currentUser, setCurrentUser] = useState<string>('');

  const connect = async () => {
    try {
      if (!Ethereum) {
        alert('Please install MetaMask');
        return;
      }
      const accounts = await Ethereum.request({
        method: 'eth_requestAccounts',
      });
      if (accounts.length === 0) {
        alert('Please unlock MetaMask');
        return;
      }
      const account = accounts[0];
      console.log('connected to account: ', account);
        setCurrentAccount(account);
    } catch (errors) {
      console.log(errors);
    }
  };

  useEffect(() => {
    if(!Ethereum){
        console.log("No ethereum wallet found, please install metamask")
        return ;
    }
    connect();
  }, []);

  useEffect(() =>{
    if(currentAccount){
      getUser();
    }
  }, [currentAccount])

const getUser = async ()=>{
  const contract = getEwitterContract();
  const user = await contract.getUser(currentAccount);
  const {avatar, bio, name, username, wallet} = user;
  console.log(user);
  return user;
}

  return { connect, account: currentAccount };
};

export default useEwitter;

# Update1 I've changed import ethers from 'ethers' to import {ethers} from 'ethers' and now I'm facing this error # Update1我已将import ethers from 'ethers'更改为import {ethers} from 'ethers'现在我遇到了这个错误

在此处输入图像描述

If unable to understand properly or if you want to see the whole codebase then this is the link to the github repo如果无法正确理解或者如果您想查看整个代码库,那么这是github 存储库的链接

https://github.com/ChiragDogra/ewitter/blob/userIssue/dapp/hooks/useEwitter.ts https://github.com/ChiragDogra/ewitter/blob/userIssue/dapp/hooks/useEwitter.ts

believe or not I just had that issue.信不信由你,我刚刚遇到了这个问题。

the problem is how you are importing ethers .问题是您如何导入ethers Should be应该

 import { ethers } from "ethers";

暂无
暂无

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

相关问题 next.js - 构建时出错:生成 static 页面(0/5)类型错误:无法读取未定义的属性 - next.js - Getting error while building: Generating static pages (0/5)TypeError: Cannot read properties of undefined Next.js 错误:无法读取未定义的属性(读取“集合”) - Next.js Error: Cannot read properties of undefined (reading 'collection') 部署智能合约时出错,无法读取未定义的“已部署”属性? - Getting an error while deploying smart contracts, Cannot read property 'deployed' of undefined? Next.js:无法读取未定义的属性(读取“indexOf”) - Next.js: Cannot read properties of undefined (reading 'indexOf') 类型错误:无法读取 Next.js 上未定义的属性(读取“调用”) - TypeError: Cannot read properties of undefined (reading 'call') on Next.js Next.js | TypeError:无法读取未定义的属性(读取“秘密”) - Next.js | TypeError: Cannot read properties of undefined (reading 'secret') 未处理的运行时错误类型错误:当我尝试使用 next.js 的图像组件时无法读取未定义的属性(读取“调用”) - Unhandled Runtime Error TypeError: Cannot read properties of undefined (reading 'call') when I try to use the Image component of next.js Mongoose 和 Next.js:未处理的运行时错误 TypeError:无法读取未定义的属性(读取“令牌”) - Mongoose and Next.js: Unhandled Runtime Error TypeError: Cannot read properties of undefined (reading 'Token') Next.js 错误:无法读取未定义的属性(读取“调用”),try/catch 不起作用 onClick? - Next.js Error: Cannot read properties of undefined (reading 'call'), try/catch won't work onClick? 不是重大错误,但我收到“TypeError:无法读取未定义的属性”。 我不确定我的下一步行动是什么 - Not a major error but I am getting `TypeError: Cannot read property 'has' of undefined`. I am not sure what my next move will be
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM