【问题标题】:my smart contract is not responding and error saying web3 is not defined我的智能合约没有响应并且错误提示 web3 未定义
【发布时间】:2021-05-02 01:41:13
【问题描述】:
[error saying web3 is not defined][1]<script>
    var myContract;
    async function CheckMetamaskConnection() {
        // Modern dapp browsers...
        if (window.ethereum) {
            window.web3 = new Web3(window.ethereum);
            try {
                // Request account access if needed
                await ethereum.enable();
                // Acccounts now exposed
                return true;
            } catch (error) {
                // User denied account access...
                return false;
            }
        }
        // Legacy dapp browsers...
        else if (window.web3) {
            window.web3 = new Web3(web3.currentProvider);
            // Acccounts always exposed

            return true;
        }
        // Non-dapp browsers...
        else {
            console.log('Non-Ethereum browser detected. You should consider trying MetaMask!');
            return false;
        }
    }

    $(document).ready(async function () {
        var IsMetamask = await CheckMetamaskConnection();
        if (IsMetamask) {
            myContract = await web3.eth.contract(SmartContractABI).at(SmartContractAddress);
            getCandidate(1);
            getCandidate(2);

            await myContract.eventVote({
                fromBlock:0
            }, function(err, event){
                console.log("event :", event);
                getCandidate(event.args._candidateid.toNumber());
            });
            console.log("myContract :", myContract);
            console.log("Metamask detected!")
        } else {
            console.log("Metamask not detected");
            Swal.fire({
              icon: 'error',
              title: 'Oops...',
              text: 'Metamask not detected!',
              onClose() {
                location.reload();
              }
            });
        }
    });

    async function getCandidate(cad){
        await myContract.candidates(cad, function(err, result){
            if (!err) {
                console.log("result : ", result);
                document.getElementById("cad" + cad).innerHTML = result[1];
                document.getElementById("cad"+cad+'count').innerHTML = result[2].toNumber();
            }
        });
    }

    async function Vote(cad){
        await myContract.vote(cad, function(err, result){
            if(!err){
                console.log("We are winning!");
            } else{
                console.log("Can not connect to the smart contract");
            }
        })
    }

</script>`

我的系统中有 node.js 和元掩码(Windows 10) 我从 github 克隆了你的项目并通过以下命令运行它

npm 安装 节点索引.js UI 在 localhost:3000 中完美部署,但是当我尝试投票时,事务不起作用!!! 然后我看到智能合约上的内容没有渲染!!! 然后我检查了 metamask,它已连接并在 ropsten 网络上有 1 个以太!!! 然后我尝试了 ganache(本地区块链提供商),但交易仍然无法正常工作!!! 然后我将智能合约粘贴到 remix 中并获取 ABI 和智能合约地址,但仍然无法正常工作!!! 然后我转到浏览器的开发人员工具并看到以下错误!!!!...我不知道这个错误!!!!...我该如何解决这个问题???

【问题讨论】:

    标签: blockchain ethereum smartcontracts web3js metamask


    【解决方案1】:

    这个错误可能是因为加载了 Web3.请试试这个功能:

    async loadWeb3(){
    if(window.ethereum){
      window.web3 = new Web3(window.ethereum)
      await window.ethereum.request({ method: 'eth_requestAccounts' })
    }
    else if(window.web3){
      window.web3 = new Web3(window.ethereum)
    }
    else{
      window.alert("Non-Ethereum browser detected. You should consider trying MetaMask!")
    }
    

    }

    另外,不要忘记在您的 javascript 类中添加导入:

    import Web3 from 'web3'
    

    并使用 npm 安装导入:

    npm i web3 --save
    

    【讨论】:

      猜你喜欢
      • 2019-08-27
      • 1970-01-01
      • 2020-01-28
      • 2021-12-30
      • 2021-10-02
      • 1970-01-01
      • 2019-11-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多