简体   繁体   English

未捕获的类型错误:web3 不是构造函数。 区块链网站无法连接到元掩码

[英]Uncaught TypeError: web3 is not a constructor. the blockchain website cannot connect to metamask

i have issue on web3.eth.defaultAccount = web3.eth.getAccounts();我有关于web3.eth.defaultAccount = web3.eth.getAccounts() 的问题;

below is the code.下面是代码。 it said Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'getAccounts')它说Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'getAccounts')

the version of my web3 is "^1.3".我的 web3 版本是“^1.3”。

var contract = "";

if (typeof web3 !== 'undefined') {
console.log('inside web3')
Web3 = new Web3 (Web3.currentProvider);
} else {
console.log('else web3');
var web3 = new Web3(new Web3.providers.HttpProvider(provider));

}

window.ethereum.enable()
.then(function (accounts) {
    console.log(accounts[0]);

    web3.eth.defaultAccount = web3.eth.getAccounts();

    var contractabi = web3.eth.contract([ABI])

function must be async function 必须是异步的

window.addEventListener("load", async () => {
  // Modern dapp browsers...
  if (window.ethereum) {
    const web3 = new Web3(window.ethereum);
    try {
      // Request account access if needed
      await window.ethereum.enable();
      // Acccounts now exposed
      resolve(web3);
    } catch (error) {
      reject(error);
    }
  }

try this to fit your case试试这个适合你的情况

if possible send me the complete implementation of this code, or you can add this如果可能,请将此代码的完整实现发送给我,或者您可以添加此代码

class App extends Component {
  async UNSAFE_componentWillMount() {
    await this.loadWeb3();
    await this.loadBlockchainData();
  }

  async loadWeb3() {
    if (window.ethereum) {
      window.web3 = new Web3(window.ethereum);
      await window.ethereum.enable();
    } else if (window.web3) {
      window.web3 = new Web3(window.web3.currentProvider);
    } else {
      window.alert('No ethereum broswer detected! You can check out MetaMask!');
    }
  }

  async loadBlockchainData() {
    const web3 = window.web3;
    const account = await web3.eth.getAccounts();
    this.setState({ account: account[0] });
    const networkId = await web3.eth.net.getId();
  }
}

please use async function to interact with web3 and await the response请使用 async function 与 web3 交互并等待响应

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

相关问题 Metamask (web3) 连接钱包并发送交易 - 如何将区块链更改为 Bianance 智能链 (BEP-20) 网络而不是以太坊? - Metamask (web3) connect wallet and send transaction - how to change blockchain to Bianance smart chain (BEP-20) network instead of Ethereum? metamask web3未定义 - metamask web3 is undefined `Uncaught TypeError: Illegal constructor.` 当扩展 HTMLSpanElement - `Uncaught TypeError: Illegal constructor.` when extending HTMLSpanElement 如何在 React 上的 bsc 测试网上将 Metamask 钱包地址连接到 web3 - How to connect Metamask wallet address to web3 on bsc testnet on React angular uing web3中如何连接钱包MetaMask? - How to connect with wallet MetaMask in angular uing web3? Web3 元掩码连接无法读取未定义的属性(读取“以太坊”) - Web3 metamask connection Cannot read properties of undefined (reading 'ethereum') [Solidity][Metamask] Metamask 不再注入 web3 - [Solidity][Metamask] Metamask no longer injects web3 MetaMask 不再注入 web3 - MetaMask no longer injects web3 VUEX & WEB3:重复的方法构造函数。 此方法定义为 RPC 调用和 Object 方法 - VUEX & WEB3: Duplicated method constructor. This method is defined as RPC call and as Object method MetaMask Web3:有什么方法可以确保网站用户连接到特定的网络? - MetaMask Web3: is there any way to make sure website user is connected to a particular network?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM