0

Issue:Not able to return the input string(ofcourse it works in remix). Below is the contract.

pragma solidity ^0.4.17;
    contract samplecontract {
    function viewuserName(string name) public returns(string){
    return (name);
}   
}

index.html:

<input id="txtdummyName" type="text" placeholder="Enter  sometext" />
<button id="btnViewUserName" onclick="App.viewUserName()">View userName</button>

when i click on the "View userName" button, ie., when i try to call the viewUserName in app.js in truffle it logs Error: VM Exception while processing transaction: revert. I tried the following commented ways too.! Also tried console.log(v.toString()) How to identify the issue?

app.js:

viewUserName:function(){    
var sometext=document.getElementById("txtdummyName").value;
sampleinstance.deployed().then(function (instance) {
  console.log(sometext); 
  // return instance.viewuserName.call(sometext).then(function(v){
  //   console.log(v);
  // })
  // return instance.viewuserName.call(sometext,{gas: 540000, from: web3.eth.accounts[0]}).then(function(v){
  //   console.log(v);
  // })
  // return instance.viewuserName(sometext,{gas: 540000, from: web3.eth.accounts[0]}).then(function(v){
  //     console.log(v);
  // })
  // return instance.viewuserName(sometext).then(function(v){
  //   console.log(v);
  // })
  return instance.viewuserName(sometext,{from: web3.eth.accounts[0],gas: 1540000}).then(function(v){
    console.log(v);
  })
})

Any help would be appreciated.

Thanks.

  • hey , instead of trying to send arguments from HTML, send a static string. if it doesn't work then it is your contract's problem. – Kaki Master Of Time May 08 '18 at 13:26
  • You function viewuserName doesn't have the view or pure modifier. So when you call it it will create a transaction instead of a call and transactions never return a value. See this https://ethereum.stackexchange.com/questions/765/what-is-the-difference-between-a-transaction-and-a-call. You need to use view or pure in your function definition. – Ismael May 08 '18 at 13:48
  • 1
    More over, return name shoud be enough. brackets not needed. – Itération 122442 May 08 '18 at 15:27
  • sending a static string didn't work – user2865883 May 09 '18 at 10:13
  • view or pure modifier also didn't work – user2865883 May 09 '18 at 10:13
  • @user2865883 are you sure you recompiled after adding those modifiers? – Joël May 10 '18 at 08:30
  • Joel, if we use truffle recompiling needs only when there is change in the contract. save will automatically refreshes the browser with app.js code – user2865883 May 10 '18 at 12:35
  • Ismael, you are saying i have to create a transaction rather than making a call. I am new to this so what exactly should i do for getting the name as the output in truffle? – user2865883 May 10 '18 at 12:36
  • @user2865883 Your function viewuserName doesn't have the pure or view modifiers. Then it will be interpreted as a transaction when used and since transactions do not return values you will not be able to get the value. If you want to read values then the function has to be declared as pure or view. If after the modification it doesn't work it is likely you are still using the old contract or the abi was not updated. To force truffle to recompile you can delete the build directory and redeploy your contracts (newer version of truffle might have fixed that bug). – Ismael May 10 '18 at 19:40
  • Thanks a lot Ismael, Making the function view and return instance.viewuserName(sometext,{gas: 540000, from: web3.eth.accounts[0]}).then(function(v){ console.log(v.toString()); }) in truffle app.js helped me out. – user2865883 May 11 '18 at 05:48

0 Answers0