Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

blockchain - my smart contract is not responding and error saying web3 is not defined

[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>`

i have node.js and metamask in my system(windows 10) i cloned you project from github and runned it by following command

npm install node index.js the UI deployed perfectly in localhost:3000 but when i try to vote the transaction is not working!!! then i saw content on smart contract is not rendering!!! then i checked metamask , which was connected and have 1 ether on ropsten network!!! then i try ganache (local blockchain provider) and still the transaction is not working!!! then i paste the smart contract in remix and get the ABI and smart contract address and still not working!!! then i goto developer tool of browser and saw below error!!!!...i have no idea of this error!!!!...how can i solve this???

question from:https://stackoverflow.com/questions/65932405/my-smart-contract-is-not-responding-and-error-saying-web3-is-not-defined

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...