0

How come when I call a function and pass it parameters, it spits out an error? I try to print the updated name (UPDATED) but it's unable to pass it back.

var Web3 = require('web3');
var web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));

var myContract = web3.eth.contract([{"constant":true,"inputs":[],"name":"get","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"x","type":"string"}],"name":"setTokenName","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"inputs":[{"name":"_tokenName","type":"string"}],"payable":false,"stateMutability":"nonpayable","type":"constructor"}])
var contractInstance = myContract.at('0x8b3c0bf558d5b25a91d8c5a5cbc48648285ad136');

contractInstance.setTokenName('UPDATED');

var updatedresult = contractInstance.get();

console.log(`updated name should be here: ${updatedresult}`);
0

You should probabaly instantiate myContract using the new operator and capitalise contract so it becomes this:

var myContract = new web3.eth.Contract([{"constant":true,"inputs":[],"name":"get","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"x","type":"string"}],"name":"setTokenName","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"inputs":[{"name":"_tokenName","type":"string"}],"payable":false,"stateMutability":"nonpayable","type":"constructor"}])

But where did you get the at function from ? It is not a function in the web3 library

var contractInstance = myContract.at('0x8b3c0bf558d5b25a91d8c5a5cbc48648285ad136');

EDIT

var Web3 = require('web3');
var web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));

var myContract = new web3.eth.Contract([{
"constant":true,
"inputs":[],
"name":"get",
"outputs":[{"name":"","type":"string"}],
"payable":false,
"stateMutability":"view",
"type":"function"},
{"constant":false,
"inputs":[{"name":"x","type":"string"}],
"name":"setTokenName",
"outputs":[],
"payable":false,
"stateMutability":"nonpayable",
"type":"function"},
{"inputs":[{"name":"_tokenName","type":"string"}],
"payable":false,
"stateMutability":"nonpayable",
"type":"constructor"}],'0x61edcdf5bb736adffe5043706e7c5bb1f1a56eea');



myContract.methods.setTokenName('UPDATED');
var updatedresult = myContract.methods.get().call({"from": 
'0x8b3c0bf558d5b25a91d8c5a5cbc48648285ad136'}).then(console.log);
console.log(`updated name should be here: ${updatedresult}`);
6
  • I'm following the Web3 JavaScript app API and it tells me to use the at function. It works properly when I do other function calls (like a get method), but calling an update method (setTokenName) causes an error – JorahFriendzone Nov 11 '17 at 0:38
  • Could you link me to the page ? Maybe I can understand better then. I am looking at the official web3.js library page and cant find anything: web3js.readthedocs.io/en/1.0/… – jold20 Nov 11 '17 at 0:54
  • Hey what version of web3 are you using? i think dependent on that, it differs on how you declare variables. I am using 1.0.0-beta.23 . I have changed the code to reflect that. Its in the edit of my answer. – jold20 Nov 11 '17 at 2:53
  • Where are you getting the "methods" statement from? – JorahFriendzone Nov 11 '17 at 20:09
0

When you read from the contract, it doesn't require gas(?), but when you write to the contract it requires gas so you need to set a defaultAccount.

Resolved using: web3.eth.defaultAccount = web3.eth.accounts[0];

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.