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.
viewuserNamedoesn't have thevieworpuremodifier. 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 usevieworpurein your function definition. – Ismael May 08 '18 at 13:48viewuserNamedoesn't have thepureorviewmodifiers. 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 aspureorview. 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