2

I am using Ganache CLI v6.1.0 (ganache-core: 2.1.0)) (started with default settings by running simply ganache-cli), web3 (^1.0.0-beta.33), and my smart contract is written for solidity ^0.4.0.

I have some functions working where I can, for instance, click a button on my web UI and have it send a transaction to my deployed smart contract and update the data on-chain. This works without any issue.

Strangely, the simple case of calling a constant function (which simply returns the length of an array) is where I'm having issues. For some reason every time I do this I get an error: Key not found in database. This error occurs in the promise returned by the call invocation.

Here is my code:

Solidity function:

function getItemsLength() public constant returns(uint256) {
    return items.length;
}

JS Code:

var account;
var numItems = 0;
web3.eth.getAccounts().then(function(result) {
account = result[0];
var contract = new web3.eth.Contract(
    simpleStorageABI,
    contractAddress,
    {
      from: account,
      gasPrice: '20000000000',
      gas: '100000'
    }
  )
contract.methods.getItemsLength().call(account) // Error occurs here      
.then(function(result) {
    numItems = result;
  })
});

I had this working using testrpc instead of Ganache a few days ago, but then moved to Ganache for websocket support (for event listeners). Somewhere along the way this broke, and it seems like perhaps a bug in Ganache.

Has anyone else run into this? I saw some issues in my searching that seemed similar but they mostly seemed intermittent and related to testing specifically. Any insight or guidance would be appreciated, I've been stuck on this for too long.

  • why you pass accounts in the call function? It has not been defined either. What value it contains? – mirg Mar 29 '18 at 17:06
  • Oops, that's a typo! Fixed – Chris Staikos Mar 29 '18 at 17:10
  • its still wrong. You need to provide the account in this way: { from: account } – mirg Mar 29 '18 at 17:16
  • Wow, thank you for that! I don't know why or when that changed, but that was the issue. I even provide the account via the options object when calling my other function (which works)... smh.. thanks! If you add an answer I will accept it – Chris Staikos Mar 29 '18 at 17:29
1

The invocation is wrong. Just try to write it in this way:

contract.methods.getItemsLength().call({ from: account })
.then(function(result) {
    numItems = result;
})

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.