【问题标题】:When I am passing array of my smart contract function it is giving me errors saying "invalid number of arguments to solidity function" Why?当我传递我的智能合约函数的数组时,它给了我一个错误,说“solidity 函数的参数数量无效”为什么?
【发布时间】:2019-09-11 10:09:25
【问题描述】:

我有一个名为 ChainList.sol 的智能合约文件,其中我有一个名为“getArticlesForSale”的函数(参见下面的代码),它返回一组索引。我还有一个 app.js 文件,我在其中使用 Promise 调用此函数(请参阅下面的代码)。但在控制台中,它给了我一个错误,说“对一个可靠函数的参数无效”。在 app.js 文件(第二个代码)中,它正在运行“catch”部分并给出错误。

在我的合同中,我尝试将 uint 转换为“uint32”,假设 javascript 无法读取大整数,即“uint256”。但仍然出现此错误。

// 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;
    });

谁能告诉我如何将一个solidity数组传递给javascript promise。

【问题讨论】:

  • 你试过return instance.methods.getArticlesForSale();吗?
  • 谢谢。实际上,我忘了删除评论,这就是我收到错误的原因。但是我注意到一件奇怪的事情,有时当我删除构建文件夹并重新编译和迁移时,它会运行。

标签: blockchain solidity web3js


【解决方案1】:

它现在可以工作了。

  • 删除“构建”文件夹。
  • 重新编译并迁移。

它现在应该可以工作了。 就我而言,我实际上忘记在一个语句中删除评论,这就是我收到此错误的原因。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-16
    • 1970-01-01
    • 2022-08-20
    • 2022-07-21
    • 2018-11-30
    • 2018-09-16
    • 2020-01-16
    • 1970-01-01
    相关资源
    最近更新 更多