0

I am attempt to get an address using methods.call(). And it's something error.

JS

var rcAddr = "0x683d1602fa7252d49cda8ac414c1641a56e73d03";
var methodName = "Access Control";
var register = new web3.eth.Contract(rcAbi, rcAddr);
var accAddr = register.methods.getContractAddr(methodName).call({
    from: "0x70f99451d9878055ca8986021ca78f6edb8f1720",
    gas: 10000000
});
console.log(accAddr)

solidity

 function getContractAddr(string memory _methodName) public view returns (address _scAddress){
        bytes32 key = stringToBytes32(_methodName);
        _scAddress = lookupTable[key].scAddress;
    }
}

I think I can get a address from function, but it's wrong. So I use console.log(accAddr) output the result.

Promise {
  _bitField: 0,
  _fulfillmentHandler0: undefined,
  _rejectionHandler0: undefined,
  _promise0: undefined,
  _receiver0: undefined }

It return something else. I'm confused. methods.myMethod.call in document say

Returns
Promise returns Mixed: The return value(s) of the smart contract method. If it returns a single value, it’s returned as is. If it has multiple return values they are returned as an object with properties and indices:

So It should return the value but why not.

  • As the documentation says, it returns a Promise, which will resolve when the call completes. Depending on where you're running this code, you can probably use await, or you can call .then on the Promise. (If none of that makes sense to you, search for a tutorial on promises or async/await in JavaScript. This concept isn't specific to Ethereum/web3.js.) – user19510 Oct 9 '19 at 7:20
0

Continuing the comment, you may want to try this:

async function func() {
    var rcAddr = "0x683d1602fa7252d49cda8ac414c1641a56e73d03";
    var methodName = "Access Control";
    var register = new web3.eth.Contract(rcAbi, rcAddr);
    var accAddr = await register.methods.getContractAddr(methodName).call({
        from: "0x70f99451d9878055ca8986021ca78f6edb8f1720",
        gas: 10000000
    });
    console.log(accAddr);
}

func(); // must be the last line
| improve this answer | |

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.