简体   繁体   English

Web3 元掩码连接无法读取未定义的属性(读取“以太坊”)

[英]Web3 metamask connection Cannot read properties of undefined (reading 'ethereum')

I have a react application in which I am trying to connect to metamask extension via web3.我有一个反应应用程序,我试图在其中通过 web3 连接到元掩码扩展。 In my thunk.ts , I am calling the required functions but I am getting a type error that I cannot read the properties of undefined(reading 'ethereum').在我的thunk.ts ,我正在调用所需的函数,但我收到一个类型错误,我无法读取未定义的属性(读取“以太坊”)。 Here is the code that I am using这是我正在使用的代码



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 !");
  }
};

Here is the error screenshot这是错误截图
这是错误截图

I solved it by using the following code我使用以下代码解决了它

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

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

相关问题 metamask web3未定义 - metamask web3 is undefined Solana web3 js keep 在尝试确认交易时无法读取未定义的属性(读取“否定”) - Solana web3 js keep gives Cannot read properties of undefined (reading 'negative') while trying to confirm transaction TypeError:无法读取未定义的属性(读取“bufferToHex”)-元掩码 - TypeError: Cannot read properties of undefined (reading 'bufferToHex') - Metamask TypeError:在设置 MetaMask 以显示 NFT 时无法读取未定义的属性(读取“地图”) - TypeError: Cannot read properties of undefined (reading 'map') while setting up MetaMask to display NFT Web3-无法读取未定义的属性“ setProvider” - Web3 - Cannot read property 'setProvider' of undefined mongoose.connection 无法读取未定义的属性(读取“on”) - mongoose.connection Cannot read properties of undefined (reading 'on') MetaMask 不注入 window.ethereum: Uncaught (in promise) TypeError: Cannot read property 'request' of undefined - MetaMask does not inject window.ethereum: Uncaught (in promise) TypeError: Cannot read property 'request' of undefined 无法读取未定义的属性(读取 *) - Cannot read properties of undefined (reading *) 无法读取未定义的属性(读取 'then') - Cannot read properties of undefined (reading 'then') 无法读取未定义的属性(读取“4”) - Cannot read properties of undefined (reading '4')
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM