Skip to content
Advertisement

Error when attempting to call smart contract with javascript

I am trying to write a piece of javascript that calls a smart contract. The smart contract has been deployed to a local test net with truffle and ganache and the javascript is producing the following error:

JavaScript

Javascript code:

JavaScript

Solidity code:

JavaScript

I am not sure if the issue is coming from me not handling the async request properly, the JSON abi being malformed or the smart contract response not being handled correctly.

Any help would be greatly appreciated.

EDIT:

I confirmed ganache is running on 8545 through the ganache output:

JavaScript

I also confirmed this through adding the following method that returned the correct block number.

JavaScript

Also I got the contract address from the truffle output contract address parameter. Is this the correct place to get a contract address?

JavaScript

I changed the script code slightly to have the console.log(Contract.methods.getName().call()); be an async call which looks like this:

JavaScript

And now I am getting the following slightly more detailed but still very confusing error:

JavaScript

Advertisement

Answer

Your contract and javascript code is correct. Error

JavaScript

indicates that you are not getting a response from the ganache.

  • Make sure that ganache is running

  • Connect to correct port. You are connecting to port “8545” but usually ganache server listens to 7545.

enter image description here

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement