【问题标题】:Web3 metamask connection Cannot read properties of undefined (reading 'ethereum')Web3 元掩码连接无法读取未定义的属性(读取“以太坊”)
【发布时间】:2021-12-26 10:19:38
【问题描述】:

我有一个反应应用程序,我正在尝试通过 web3 连接到元掩码扩展。在我的thunk.ts 中,我正在调用所需的函数,但我收到一个类型错误,我无法读取未定义的属性(读取“以太坊”)。这是我正在使用的代码



if (window.ethereum) {//the error line
      window.web3 = new Web3(window.ethereum);

    try {
      await window.ethereum.enable();

      updateAddress(dispatch);
    } catch (err) {
      alert("Something went wrong.");
    }
  } else if (window.web3) {
    window.web3 = new Web3(window.web3.currentProvider);
    updateAddress(dispatch);
  } else {
    alert("You have to install MetaMask !");
  }
};

这是错误截图

【问题讨论】:

    标签: javascript reactjs typescript redux-thunk web3


    【解决方案1】:

    我用下面的代码解决了

    declare global {
      interface Window {
        ethereum?: any;
        web3?:any
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2022-06-14
      • 2023-02-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-11
      • 2022-01-15
      • 2021-12-31
      • 2021-12-13
      • 2021-11-28
      相关资源
      最近更新 更多