1

Hi I'm having a web3 code, web3 version is 0.20.0

MyContract.addPerson.call("0x6b1c97bf430704e118cb3aaeb2bccce1a29d85e3",function (error, result){
            if (error) {
                console.log('Error:'+JSON.stringify(error));
                res.send({'status':0,'message':error});
            }else{
                console.log('Result:'+JSON.stringify(result));
                res.send({'status':1,'message':result});
            }
        });

My contract code is

function addPerson(address pesonAddress) public returns(address message){
        if(msg.sender != owner) {message = 0; revert();}
        if(auctioneers[pesonAddress] == true){
            message = 0;
        }else{
            auctioneers[pesonAddress] = true;
            auctionersList.push(pesonAddress);
            message = pesonAddress;
        }

    }

Compiler version is 0.4.20.

Also I tried with

MyContract.addPerson("0x25c8b176c11e21fe5ffdc943acdf6d070d197e02",{value:0,gas: 30000},function (error, result){
            if (error) {
                console.log('Error:'+JSON.stringify(error));
                res.send({'status':0,'message':error});
            }else{
                console.log('Result:'+JSON.stringify(result));
                res.send({'status':1,'message':result});
            }
        });

It shows addPerson() is not a function.

It works well when I test it in remix IDE. But when I call addPerson() with address as an parameter it doesn't add that address to the map and array.Also I checked the address by returning the address, it matches. Also I add an address in remix IDE and then I returned that array in web3js, it contains array of addresses that I added in remix IDE. What I'm doing wrong here? Thank you.

  • how do you create the contract instance? – mirg Mar 15 '18 at 14:23
  • Contract is working fine when I call other function. But not this addPerson() – e.k Mar 16 '18 at 5:05
1

Calling your method in this way : MyContract.addPerson.call(...) is just a readonly interaction. And it is used to only return data, not update it.

In order to actually change storage ( add an item into an array or update a variable ) you need to call addPerson this way : MyContract.addPerson(...) without the call

| improve this answer | |
  • Yes I also tested like that. In that case it shows addPerson() is not a function. I updated my question please look into it. – e.k Mar 15 '18 at 14:06
  • can you post the whole code where you call the function in javascript – Kaki Master Of Time Mar 15 '18 at 14:31
  • I updated my question. – e.k Mar 16 '18 at 5:04
  • Instead of call() what should I use read write interaction – e.k Mar 16 '18 at 5:11
  • you can use sendTransaction for functions who changes the state. But, in case you use yourContract.method web3 is able to define whether to use call or sendTransaction. github.com/ethereum/wiki/wiki/JavaScript-API#web3ethcontract – mirg Mar 16 '18 at 5:41

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.