簡體   English   中英

使用 web3 調用智能合約函數的問題

[英]Issue in calling a smart contract function with web3

我正在嘗試調用此智能合約中提供的 createCustomer 函數https://ropsten.etherscan.io/address/0xD3B462CbF6244ed21CD3cF334Bf8CB44A28795A9#code

我們基本上必須提供三個參數,如字符串內存_hashedEmail、字符串內存_name和字符串內存_phone。

所以我編寫了以下程序來調用 createCustomer 函數

const addcustomer = async (req, res, next) => {
  try {
        const init = async() => {
            const provider = new  HDWalletProvider(
              privateKey1,
              'https://ropsten.infura.io/v3/1693cef23bd542968df2435f25726d39'
            );
            const web3 = new Web3(provider);

            let contract = new web3.eth.Contract(abi2, address3);
            contract.methods.createCustomer({_hashedemail: "a", _name: "nike", _phone: "99"}).call((err, result) => { console.log(result) });
            };
            init(); 
    }catch (err) {
        //throw error in json response with status 500. 
        return apiResponse.ErrorResponse(res, err);
    }
};

但是它給了我這個錯誤,這沒有任何意義,因為我已經提供了三個參數。

(node:14744) UnhandledPromiseRejectionWarning: Error: Invalid number of parameters for "createCustomer". Got 1 expected 3!

刪除createCustomer中的{ } 所以要清楚,這一行:

contract.methods.createCustomer({_hashedemail: "a", _name: "nike", _phone: "99"})

應該:

contract.methods.createCustomer(_hashedemail: "a", _name: "nike", _phone: "99")

另外,當您嘗試與修改區塊鏈的函數進行交互時,您不應該使用.call而是使用.send來調用它。

要了解更多信息,您應該查看 web3.js 文檔

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM