0

In Solidity, I have an array declared as follows:

uint[] public moveTimeStamp; 

In web3 JavaScript, I will like to retrieve, say, the 0th element in the array. I tried to write it like this:

var contract = web3.eth.contract(interface);
var instance = contract.at("0x1234...");
instance.moveTimeStamp(0).call(function(err, result) {....

Web3 says this isn't correct. What should I be writing after moveTimeStamp?

I tried the following:

instance.moveTimeStamp(0)
instance.moveTimeStamp([0])

...which doesn't work.

This is how I call it:

var contract = web3.eth.contract(interface); 
var instance = contract.at(contractaddress);
instance.callTimeStamp(0).call(function(err, result) {... 

And this is the error I get

TypeError: instance.callTimeStamp(...).call is not a function 

And if you will like see that the callTimeStamp array really does contain something, you can go to https://ropsten.etherscan.io/address/0x86c9d3abf13d25ced87c0d00331667ac44c04a89#readContract to read the contract variables.

  • "Web3 says this isn't correct." Does that mean you're seeing an error message? If so, what's the error? – smarx Aug 1 '18 at 18:03
  • Added the error message and the link to my contract on Ropsten. – Jackson Ng Aug 2 '18 at 2:21
0

I'm not sure if creating an instance changes the semantics of how call works, but based on the web3 1.0 docs it looks like web3 expects the call to be shaped like:

<contract>.methods.<methodName>([...methodArgs]).call(callback)

Otherwise it looks like your call is correct, the Solidity docs seem to suggest that a getter for an array takes a uint for the requested index.

  • This is how I call it: var contract = web3.eth.contract(interface); var instance = contract.at(contractaddress); instance.callTimeStamp(0).call(function(err, result) {... And this is the error I get TypeError: instance.callTimeStamp(...).call is not a function And if you will like see that the callTimeStamp array really does contain something, you can go to ropsten.etherscan.io/address/… to read the contract variables. – Jackson Ng Aug 2 '18 at 2:17
  • The getter method will have the same name as the variable declared, so your original .moveTimeStamp(0) was correct. What happens if you change it to instance.methods.moveTimeStamp(0).call()? – ohsully Aug 2 '18 at 6:48

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.