2

I've deployed the following contract to Ethereum testnet.

contract demo {
  string public name = "someString"; 

  function returnValue() constant returns (string){ 
      return name;    
  }
}

I am trying to retrieve the contract's string and this won't work

var contract = web3.eth.contract(contractAddress, abi); //contractAddress+abi known
console.log(contract.returnValue);

returns undefined

If I use this code:

var asdf = web3.eth.contract(abi).at(contractAddress);
alert(asdf.returnValue);

I get `function(){[native code]} How can i get the string "name" from my solidity contract to my javascript? Thank you

2

1 Answer 1

5

Try this:

var contract = web3.eth.contract(abiArray).at(contractAddress);
contract.returnValue.call().then(function(v) {
    var strName= v.toString();
    console.log("Name: "+ strName);   
});

This is how I do using truffle:

import demo_artifacts from './../../build/contracts/demo.json';  
var Demo = contract(demo_artifacts);

window.getValue=function(){  
    Demo.deployed().then(function(contractInstance) {  
        contractInstance.returnValue.call().then(function(v) {  
            var strName= v.toString();  
            console.log("Name: "+ strName);
        });    
    });  
}    
window.getValue();  

Try this one:

var abiDef = "(provide the abi)";   
var contractAddress = "(provide the contract address)";  
var objWeb3 = new Web3(new
Web3.providers.HttpProvider("http://localhost:8545"));   
var democontract = objWeb3.eth.contract( JSON.parse(abiDef)).at(contractAddress);  
var callData=democontract.returnValue.call();
console.log(callData); // Printed: BigNumber {s: 1, e: 3, c: Array[1]}  
console.log(callData.c[0]); // Printed: the value in my case 1000  
4
  • this is what I get Uncaught TypeError: Cannot read property 'call' of undefined
    – PetrosM
    Commented Apr 3, 2017 at 17:29
  • Try updated code
    – Susmit
    Commented Apr 3, 2017 at 18:22
  • I am not using truffle. I use the web3 Javascript API, that probably why the call() function won't work
    – PetrosM
    Commented Apr 3, 2017 at 19:54
  • Added another version try that
    – Susmit
    Commented Apr 4, 2017 at 5:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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