0

I'm the new to web3.

I wanna create the system that when I input the string, then I can get the data of the string I saved before.

but when I click the button and I want the result displayed,

it didn't work and I get the error: "Invalid number of arguments to Solidity function"

in the line of search.js: "contract.search(input_company).call(function(err, result){..."

anyone know what happened? Is it the problem of the version conflict?

and the html and js code shown as following:

search.html:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Candiates</title>
    <!--include css files here-->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
  </head>

  <body>
    <div class="container-sm mt-5" style="width: 50%;">

      <div class="row mb-3 text-center">
        <h1>Company</h1>
      </div>


      <div class="row">
        <div class="col-10 m-auto mb-5">
          <label for="company">Input the Company Name</label>
          <input class="form-control" id="company" name="company"></input>
        </div>

        <div style="text-align: center;">
          <button class="btn btn-primary" id="button_formCompany" onclick="showCompanyResultLineChart()">Search</button>
          <a href="./home.html" class="btn btn-primary">Back</a>
        </div>

      </div>

      <div class="row">
        <div id="companyResultLineChart">
        </div>
      </div>


    </div>

    <script src="../function/web3.min.js"></script>
    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"
      integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"
      integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"
      integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
    <script src="../function/search.js"></script>

  </body>
</html>

search.js:

document.write('<script src="../function/web3.js"></script>');

//get the formCarbonEmission data
function showCompanyResultLineChart() {
  const input_company = document.getElementById("company");
  let companyResultLineChart = document.getElementById("companyResultLineChart");

  companyResultLineChart.innerHTML = `<h4>Company Results</h4>`;

  contract.search(input_company).call(function(err, result){
    if (err) {console.log(err);}
    if (result) {
        companyResultLineChart.innerHTML = "the" + search_resource + 
        " factory created " + result[0] + " tons of CO2 in " + result[1];}
  });
}

and the solidity code is the following:

contract Storage{
   
    string b = "calculation a";
    struct UserInfo {
        string time;
        string source;
        uint emission;
    }
    mapping (string => UserInfo) users;
    
    function set(string memory input_time, string memory input_source, string memory input_mycalculate, uint input_material) public {
        users[input_source].time = input_time;
        if (keccak256(abi.encodePacked(input_mycalculate)) == keccak256(abi.encodePacked(b)))
            users[input_source].emission = input_material * 3 / 2;
        else
            users[input_source].emission = input_material * 5 / 2;
    }
    
    function search(string memory input_source) public view returns (uint, string memory) {
        return (users[input_source].emission, users[input_source].time);
    }
}

and the following code is web3.js:

// Initialize Web3
var web3 = new Web3(new Web3.providers.HttpProvider('http://localhost:7545'));
/* if (typeof web3 !== 'undefined') {
    web3 = new Web3(web3.currentProvider);
} 
else {
    web3 = new Web3(new Web3.providers.HttpProvider('http://localhost:7545'));
} */

// Set Account
web3.eth.defaultAccount = web3.eth.accounts[0];

// Set Contract Abi
var ABI = [abi];

// Set Contract Address
var contractAddress = 'address';

// Set the Contract
var contract = web3.eth.contract(ABI).at(contractAddress);
3
  • which version of web3js are you using? and where does 'contract.' in js script come from? you usally need to initialize the contract first and then call method. web3js.readthedocs.io/en/v1.4.0/…
    – Majd TL
    Aug 11, 2021 at 6:44
  • the version of the web3 is 0.19.0 and I've added the code of web3.js in the article, where the 'contract' comes from.
    – ginwei
    Aug 12, 2021 at 6:23
  • use new version of web3 please, 0.19.0 is very very old
    – Majd TL
    Aug 12, 2021 at 7:04

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.