1

I have declared one array inside solidity contract like "uint256[] public machineList; " and initialize the values of that array inside the constructor.

But when I call that array inside js usgin web3 mentioned as below :

AssetContract.deployed().then(function(contractInstance) {

    let getMachineList = contractInstance.machineList;
    alert(getMachineList);
});

It is returning below output in getMachineList variable:

    function () {
  var instance = this
  var args = Array.prototype.slice.call(arguments)
  var tx_params = {}
  var last_arg = args[args.length - 1]
  if (Utils.is_object(last_arg) && !Utils.is_big_number(last_arg)) {
    tx_params = args.pop()
  }
  tx_params = Utils.merge(C.class_defaults, tx_params)
  return C.detectNetwork().then(function () {
    return new Promise(function (accept, reject) {
      var callback = function (error, result) {
        if (error != null) {
          reject(error)
        } else {
          accept(result)
        }
      }
      args.push(tx_params, callback)
      fn.apply(instance.contract, args)
    })
  })
}

this type of data I am getting in alert(contractInstance.machineList);

So, how can we get array values in side JS without making explicit getter function inside solidity rather to call in built in getter function which is provided by solidity for state variable ?

0

You can get dynamic array like this : uint256[] public machineList by creating a contract getter function.

function getMachineList () external returns (uint256[]){
return machineList
}

note the external modifier i added, in this kind of functions where you are returning a dynamically sized array the external modifier is mandatory. it also makes the function only callable from the exterior of the contract which is exactly what we want here.

An other thing is that you are using truffle to call contract functions. When using truffle you need to call a promise (or a callback) after the name of the function :

AssetContract.deployed().then(function (contractInstance) {

  contractInstance.getMachineList.
  call(**arguments if any**,{ from:**address**, gas: 300000 }).then(
    data => {
      var MachineList = data;
      alert(MachineList);
    }
  )

})

Note: whenu sing truffle you add the .call() if you are just calling functions that read and return variables as if it will not alter state variables (change/add existing/new value).

  • Yes, actually your solution is correct, I have already make the getter function which returning that array. and also I got the proper values in side the js file like this. contractInstance.getMachineList.call().then(function(machineId) { var temp = contractInstance.machineList; alert(temp); }); – Riya Soni Apr 10 '18 at 9:56
  • But the point I didn't get is that if we are returning an array then why I got the output like this : 111, 112, 113,114,115,116 instead of array object. – Riya Soni Apr 10 '18 at 9:57
  • Actually later on I got that in solidity in built getters are provided that's why I have tried out solution which is there in question – Riya Soni Apr 10 '18 at 9:58
  • yes, public state variables have getters created automatically, but i am not sure that you can call get on a public dynamic array and still get the values you need. But the external getter function i mentioned returns the correct values and format, if you can post more code and output then i can help clarrify. – Kaki Master Of Time Apr 10 '18 at 10:10
  • 1
    Means I can change the value of mapping in one function and returning it in another function. Will that work ? – Riya Soni Apr 10 '18 at 13:11
0

You can't access to the entire array without specifying the getter. You can still call the build in function provided by the public state variable but you need to provide the index of the array to get its value.

Indeed, your ABI for the provided array would be something like this

{
    "constant": true,
    "inputs": [
        {
            "name": "",
            "type": "uint256"
        }
    ],
    "name": "machineList",
    "outputs": [
        {
            "name": "",
            "type": "uint256"
        }
    ],
    "payable": false,
    "stateMutability": "view",
    "type": "function"
}

an input is expected, and is the index of the element of the array that you want to read.

  • Yes, if we are providing index then it is returning [object Promise] type of value. – Riya Soni Apr 10 '18 at 10:00

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.