1

I am quite new with javascript web3 so don't be judge too hard.

I am using web3 version 1.0.0 beta.

I am taking the web3 reference this way:

if (typeof web3 !== 'undefined') {
    // If a web3 instance is already provided by Meta Mask.
    web3Provider = web3.currentProvider;
    web3 = new Web3(web3.currentProvider);
} else {
    // Specify default instance if no web3 instance provided
    web3Provider = new Web3.providers.HttpProvider('https://rinkeby.infura.io/v3/1b07b6c661304ef48005e16b43b7efa9');
    web3 = new Web3(web3Provider);
}

And then i am trying to get the contract reference:

var abi = [...];

console.log(abi);

var myContract = web3.eth.contract(abi, "0x2a2a7c53a6cc3d775e80c38d0fc446e73078902f");




myContract.methods.getPoll(1)
.call({from:localAccount}, function(error, result){
    console.log('error: ' + error);
    console.log(result);
})

The error is: Cannot read property 'getPoll' of undefined

0

You aren't creating the contract object correctly. What version of Web3.js are you using? 1.0 or 0.2.x? Here is how you do it for each:

1.0: var myContract = new web3.eth.Contract(abi, "0x2a2a7c53a6cc3d775e80c38d0fc446e73078902f")

0.2.x: var myContract = web3.eth.contract(abi).at("0x2a2a7c53a6cc3d775e80c38d0fc446e73078902f")

I highly suggest you use 1.0 if you aren't yet, even though it's in beta.

  • I tried this " var myContract = web3.eth.contract(abi).at("0x2a2a7c53a6cc3d775e80c38d0fc446e73078902f");" and i get the same error. – Alexandru Sandu Dec 4 '18 at 16:52
  • And about the version, i think it is 1.0.0 beta. I am importing it this way: "<script src="cdn.jsdelivr.net/gh/ethereum/web3.js@1.0.0-beta.36/dist/…" integrity="sha256-nWBTbvxhJgjslRyuAKJHK+XcZPlCnmIAAMixz6EefVk=" crossorigin="anonymous"></script>" – Alexandru Sandu Dec 4 '18 at 16:54
  • That's not what I put though. Try what I put in my answer, var myContract = new web3.eth.Contract(abi, "0x2a2a7c53a6cc3d775e80c38d0fc446e73078902f") Glad to hear you're using 1.0 though! – flygoing Dec 4 '18 at 17:11
  • I made it work, apparently web3 version was 0.2.x taken from metamask. Now that i am using 1.0.0 it works. At least that part... – Alexandru Sandu Dec 11 '18 at 13:16
0

Change this:

web3.eth.contract

To this:

new web3.eth.Contract
  • 1
    “Web3.eth.Contract is not a constructor” or something similar – Alexandru Sandu Dec 4 '18 at 17:09
  • @AlexandruSandu: web3, not Web3! – goodvibration Dec 4 '18 at 17:14
  • I know, i tried that but i wrote this comment form my phone and misspelled it – Alexandru Sandu Dec 4 '18 at 17:15
0

You must write it in this form:

if getPoll method is view or pure:

myContract.methods.getPoll(1).call(option,function(error,result){
    if (! error)
        console.log(expand(result));
    else
        console.log(error);
});

and if the method is changing the state variables use send method, instead of call.

option is a variable that clarify the transaction specifications, like:

var option = {from: 0x...};
  • Still undefined – Alexandru Sandu Dec 8 '18 at 17:04
  • Is your ABI code correct? – Alireza Zojaji Dec 8 '18 at 18:01
  • Yes, i am using the same json with the truffle-contract method of getting the contract, which works fine (“$.getJSON(yadayadayada)”) – Alexandru Sandu Dec 8 '18 at 18:10

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.