Search code examples
blockchainsolidityweb3js

When I am passing array of my smart contract function it is giving me errors saying "invalid number of arguments to solidity function" Why?


I have smart contract files named as ChainList.sol where I have one function named as "getArticlesForSale"(see the below code) which returns an array of indexes. I have one more app.js file where I am calling this function using promise(see the below code). But in console, it is giving me the error saying "Invalid arguments to a solidity function". In app.js file(second code), It is running "catch" part and giving error.

In my contract, I have tried converting uint to "uint32" assuming that javascript cannot read big integer i.e. "uint256" in solidity. But still getting this error.

// ChainList.sol file
// fetch and return all article IDs which are still for sale
  function getArticlesForSale() public view returns (uint[] memory) {
    // prepare an output array
    uint[] memory articleIds = new uint[](articleCounter);

    uint numberOfArticlesForSale = 0;
    // iterate over all the articles
    for (uint i = 1; i <= articleCounter; i++) {
      if (articles[i].buyer == address(0)) {
        articleIds[numberOfArticlesForSale] = articles[i].id;
        numberOfArticlesForSale++;
      }
    }

    // copying article ids into smaller forSale array
    uint[] memory forSale = new uint[](numberOfArticlesForSale);

    for (uint j = 0; j < numberOfArticlesForSale; j++) {
      forSale[j] = articleIds[j];
    }

    return forSale;
  }
// app.js file, interacting with my smart contract
App.contracts.ChainList.deployed().then(function(instance){
      chainListInstance = instance;
      return instance.getArticlesForSale();
    }).then(function(articleIds){

      // retrieve the article placeholder and clear it
      $('#articlesRow').empty();

      for (var i = 0; i < articleIds.length; i++) {
        var articleId = articleIds[i];
        chainListInstance.articles(articleId.toNumber()).then(function(article){
          // 0 is id, 1 is seller, 3 is name, 4 is description, 5 is price
          App.displayArticle(article[0], article[1], article[3], article[4], article[5]);
        });
      }

      App.loading = false;

    }).catch(function(err){
      console.error(err.message);
      App.loading = false;
    });

Can anyone tell me how can I pass an array of solidity to javascript promise.


Solution

  • It is now working.

    • Delete the "build" folder.
    • Re-compile and Migrate.

    It should work now. In my case, I actually forgot to remove the comment in one statement and that's why I was getting this error.