I am working on a simple Truffle project that just shows "Yerevan is love". My contract code:

pragma solidity ^0.5.12;
contract TEST{
   bytes note="Yerevan is love";
   function fname() public view returns(string memory){
      return string(note);

And by app.js i could create a communication between the contract and JavaScript but i don't know what is an appropriate code to show "Yerevan is love" in Node-js .My app.js code:

var Web3=require("web3");
var web3= new Web3('ws://localhost:8545');
var fs=require('fs') ;
var jsonFile="C:/Users/lenovo/node_modules/Yerevan/build/contracts/TEST.json";
var parsed=JSON.parse(fs.readFileSync(jsonFile));
var abi=parsed.abi;
var myContract= new 
    web3.eth.Contract(abi,"0xEC3B8e1607C92D5695dE5FaCdE3db76EBff7f5eA" );

This app.js has not a code to show "Yerevan is love" because i don't know that. Please provide the appropriate code and a guide for a beginner man.


Here is a working js script example of your contract:

var Web3 = require('web3')
let abi = [
        "constant": true,
        "inputs": [],
        "name": "fname",
        "outputs": [
                "internalType": "string",
                "name": "",
                "type": "string"
        "payable": false,
        "stateMutability": "view",
        "type": "function"

let addressOfContract = '<PUT_ADDRESS_OF_CONTRACT_HERE>'
let endPoint = '<PUT_YOUR_ENDPOINT_HERE>'

// Destinguishes browser from server side
if (typeof window !== "undefined" && typeof window.web3 !== "undefined") {
    // we are in the browser and metamask injected a provider and is running
    web3 = new Web3(window.web3.currentProvider);
} else {
    // we are in the server side and metmask is not available
    const provider = new Web3.providers.HttpProvider(
    web3 = new Web3(provider);

testContract = new web3.eth.Contract(abi, addressOfContract)

  • Thank you, Is there any code to see the result of testContract.methods.fname().call().then(console.log) in browser by Node-js or another tool? Is an appropriate code for that?
    – Alireza
    Feb 12 '20 at 15:10

You should add the below command after the last line you embedded in the question:


With the JavaScript code you provided in the question, you just can define the given contract. You need to use another method of web3 that executes a solidity function. For executing a solidity function with web3 methods you should try a command like this:

contract_name.methods.function_name(<arguments ...>).send({from:<sender address>, value:<ether values>, gas:<gas value to be used>, gasPrice:<price and fee for a gas used>});


contract_name.methods.function_name(<arguments ...>).call();

The first command changes state and is appropriate to setter functions. The second command doesn't change state and is appropriate to getter functions. After that, you can use .then(console.log) to see the result in console. So, the solution is one of followed commands:

contract_name.methods.function_name(<arguments ...>).send({from:<sender address>, value:<ether values>, gas:<gas value to be used>, gasPrice:<price and fee for a gas used>}).then(console.log);


contract_name.methods.function_name(<arguments ...>).call().then(console.log);
  • You are missing the parenthesis after the function name. It should be like this contract_name.methods.function_name().call(). The parenthesis are required, even when the function doesn't take any input parameters.
    – Ismael
    Apr 20 at 14:36
  • 1
    @Ismael Thanks for your pointing. I edited that.
    – Kiasha
    Apr 20 at 17: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.