I am trying to do a contract call with some parameters. I do this with a NodeJS Express API.

The code looks as following:

var array = JSON.parse(req.body.arguments);
                getContractFromABIAndAddress(req.body.abi, req.body.contractAddress, function(contract) {
                    contract.methods[req.body.method](array).send({from: config.publickey}).on('receipt', function(receipt) {
                            txHash: receipt.transactionHash,
                            contractAddress: receipt.contractAddress,
                            txURL: config.etherscantransaction + receipt.transactionHash,
                            contractURL: config.etherscanaddress + receipt.contractAddress

The req.body.arguments contains the needed parameters and the req.body.method contains the right method to call on the contract.

I can test the code with postman, the body would look like the following picture.

Image of post with postman

Now as you can see here: Web3 Contract docs, I am using the second option. myContract.methods['myMethod(uint256)'](123)

The problem is that I can't send multiple parameters, the contract needs the parameters like this: myContract.methods['myMethod(uint256,uint256)'](2,2) but I am giving them like this myContract.methods['myMethod(uint256,uint256)']([2,2]).

This results in the following: enter image description here

Which means that the contract call is only getting one parameter according to the error logs and to the web3-eth-contract library. This can be solved by doing the following: enter image description here

Which turns my parameters back to the right format: enter image description here

If the Web3 library would get an update now, my NodeJS API wouldn't work anymore because I have been messing with the library. I can't find any other way to give the parameters with the contract call, or with contract send, since the problem also exists over there.

Could someone help me out?


I believe this should work:

contract.methods[req.body.method].apply(contract, array)

See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/apply.

  • Then contract.methods[req.body.method] is undefined... that problem seems unrelated to the use of apply. Make sure that req.body.method is the name of a method that actually exists on the contract. (EDIT: This was in response to a comment that has since been deleted.)
    – user19510
    Mar 15 '18 at 19:06
  • Hi, thanks for your answer. However, this is not the solution for me. I now get the following error: Error: Invalid number of parameters for "myMethod". Got undefined expected 2! Code is looking as follows: contract.methods[req.body.method].apply(contract, array).send({from: config.publickey}).on('receipt', function(receipt) {
    – Billy
    Mar 15 '18 at 19:06
  • 1
    Did you remove your change to web3-eth-contract?
    – user19510
    Mar 15 '18 at 19:07
  • Sorry, it has been a very long day haha. Thank you so much!
    – Billy
    Mar 15 '18 at 19:10

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.