[Ethereum] web3 1.0.0 method call doesn’t work

dapp-developmentweb3js

I've successfully deployed on Ganache test network a contract with this test method in it:

function test() public pure returns (uint) {
    return 7;
}

In my JS script I try to call this method in this way:

var abi = JSON.parse('...'); // 
var contract = new web3.eth.Contract(abi, '0xf1447c6d7Bff526411fFfC68502017Ce9a0AE54a'); // contract mined at this address
contract.methods.test().call().then(function(e,r) {
    console.log(e,r);
});

The console.log gives me this result (instead of 7):

false, undefined

I don't think it's an address problem (i've tried to change it and tthe script raises an error before call the method).

Any idea?

Best Answer

It's hard to detect what's wrong from your code. Formally seems right, if the address is correct what about the abi? If you can post the whole code (or a way to reproduce it) might be more easy to understand what's wrong.

I've used web3 1.0 recently and I had no issue on calling contract methods as per doc

// Solidity
contract MyContract {
    function myFunction() returns(string myString) {
        return "Hello!%";
    }
}

// web3.js
var MyContract = new web3.eth.contract(abi, address);
MyContract.methods.myFunction().call()
.then(console.log);
> "Hello!%"

https://web3js.readthedocs.io/en/1.0/web3-eth-contract.html#id15