0

I am creating a DApp where I want to show the public variable's information from the contract. I want my users who don't have metamask to see this information also. Is there a way I can do that?

If I use the DApp without using metamask then it returns that 'web3' is not defined.

Here is my code snippet of app.js when page loads [throws error at line 3]:

$(document).ready(function() {
// Ropsten:
var contractAddress = "0x208122Dc624EF89fD1960B060775439494FAa9ab";

var version = web3.version; // throws error at this line.
console.log(version);
// Modern dapp browsers...
if (window.ethereum) {
    window.web3 = new Web3(ethereum);
    try {
        // Request account access if needed
        ethereum.enable();
    } catch (error) {
        // User denied account access...
        console.log(error);
    }
}
// Legacy dapp browsers...
else if (window.web3) {
    window.web3 = new Web3(web3.currentProvider);
}
// Non-dapp browsers...
else {
    console.log('Non-Ethereum browser detected. You should consider trying MetaMask!');
}

window.ethereum.on('accountsChanged', function(accounts) {
    updateDetails(accounts[0]);

});

Contract = new web3.eth.Contract(abi, contractAddress);
console.log(Contract);
});

Error:

Uncaught ReferenceError: web3 is not defined at HTMLDocument. (app.js:6) at i (jquery-2.2.4.min.js:2) at Object.fireWith [as resolveWith] (jquery-2.2.4.min.js:2) at Function.ready (jquery-2.2.4.min.js:2) at HTMLDocument.J (jquery-2.2.4.min.js:2)

  • throws error at line 3 - what is line 3 and what is the error? – goodvibration Dec 30 '19 at 10:36
  • I have updated the question with the error and the error line – Fariha Abbasi Dec 30 '19 at 11:32
  • Please specify which line of code (i.e., post the actual code, and no images please, just plain text). – goodvibration Dec 30 '19 at 11:44
  • it is throwing error that "web3 is not defined" and line 3 i.e. (var version = web3.version;) please check the code snippet as well. – Fariha Abbasi Dec 30 '19 at 11:45
  • 1
    With regards to the line var version = web3.version - it is not clear why you are calling this line before instantiating the web3 object. It is also not clear what role window.web3 (in opposed to web3) has in your code. Finally, though not directly related, it is not clear why you reinitialize the window.web3 object if it exists (else if (window.web3) window.web3 = ...)! – goodvibration Dec 30 '19 at 12:00
0

In the case you like to use web3 without Metamask you have to include web3.js for the browser and connect with a provider (here ganache on local host):

    <!DOCTYPE html>
    <meta content="text/html;charset=utf-8" http-equiv="Content-Type">
    <meta content="utf-8" http-equiv="encoding">
    <html>
    <head>
        <title>Using web3 API with MetaMask</title>
        <script src="https://cdn.jsdelivr.net/npm/web3@1.2.2/dist/web3.min.js"></script>
        <script>
            window.addEventListener('load', function () {
                if (!window.ethereum) {
                    window.web3 = new Web3(new Web3.providers.HttpProvider('http://127.0.0.1:8545'));
                    window.web3.eth.getAccounts(function (err, acc) {
                        if (err != null) {
                            self.setStatus("There was an error fetching your accounts");
                            return;
                        }
                        if (acc.length > 0) {
                            console.log(acc);
                        }
                    });
                }
            });
        </script>
    </head>
    </html>

Expected console output of browser is:

enter image description here

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.