[Ethereum] contract.at is not a function


I'm learning about ethereum and smart contracts and I have a simple contract that stores a string. I'm trying to use web3js to change that string. I'm having trouble following the instructions from this answer https://ethereum.stackexchange.com/a/12932. This works fine when I'm in the geth console but when I try to make a js script or use the node console it's like .at doesn't exist. This is my output.

> var contractAbi = new web3.eth.Contract(abi);
> var myContract = contractAbi.at(contractAddress);
TypeError: contractAbi.at is not a function
    at repl:1:30
    at sigintHandlersWrap (vm.js:22:35)
    at sigintHandlersWrap (vm.js:73:12)
    at ContextifyScript.Script.runInThisContext (vm.js:21:12)
    at REPLServer.defaultEval (repl.js:340:29)
    at bound (domain.js:280:14)
    at REPLServer.runBound [as eval] (domain.js:293:12)
    at REPLServer.<anonymous> (repl.js:539:10)
    at emitOne (events.js:101:20)
    at REPLServer.emit (events.js:188:7)
    at REPLServer.Interface._onLine (readline.js:232:10)
    at REPLServer.Interface._line (readline.js:583:8)
    at REPLServer.Interface._ttyWrite (readline.js:860:14)
    at REPLServer.self._ttyWrite (repl.js:612:7)
    at ReadStream.onkeypress (readline.js:119:10)
    at emitTwo (events.js:106:13)
    at ReadStream.emit (events.js:191:7)
    at emitKeys (internal/readline.js:389:14)
    at next (native)
    at ReadStream.onData (readline.js:970:36)
    at emitOne (events.js:96:13)
    at ReadStream.emit (events.js:188:7)
    at readableAddChunk (_stream_readable.js:176:18)
    at ReadStream.Readable.push (_stream_readable.js:134:10)
    at TTY.onread (net.js:547:20)

Has anyone had this problem and knows of a fix?

Best Answer

Your commands have mixed Web3 1.0.x beta commands (your first line) with Web3 0.x.x commands (second line).

See below for two versions on how you can get this to work, assuming your change string function is "changeString":

Web3 1.0.x beta | web3.eth.Contract

var contractInstance = new web3.eth.Contract(abi, _contractAddress);

// use .send() to send the transaction to the blockchain and change state
contractInstance.methods.changeString('new string').send({ from: _yourAccount })
  .then(receipt => { /** some action **/ });

Web3 0.x.x released | web3.eth.contract

var MyContract = web3.eth.contract(abi);
var contractInstance = MyContract.at(_address);
contractInstance.changeString('new string',
  { from: _myAccount, gas: _gasLimit },
  (err, res) => { /** callback **/ }
Related Topic