0

When i use some web3 options as follows, I receive a common error i.e.

TypeError: web3.[option] is not a function

For example :

TypeError: web3.toAscii is not a function

web3.toAscii(0x37361fd8eb5f78c7558ccfaa338c1935ecbc3d486f7a63c5b3e1b245779bc90e.input)
TypeError: web3.toAscii is not a function

And I receive same error using another format of this command as follows :

var str = web3.toAscii("0x37361fd8eb5f78c7558ccfaa338c1935ecbc3d486f7a63c5b3e1b245779bc90e");
console.log(str);

Or

TypeError: MyContract.MyEvent is not a function

var Ev = contractInstance.LogNewObject({}, {fromBlock: 0, toBlock: 'latest'});
       Ev.get((error, events) => {
       if (!error) {
          function ShowResults(event) {
            console.log('sub_states_types: ' + event.args.sub_states_types);
            console.log('sub_states_values: ' + event.args.sub_states_values);
            }
          events.forEach(ShowResults);
        } else {
        console.log('Error');
          }
        });

And my smart contract is as follows :

pragma solidity 0.4.23; 

contract RFID {

    struct StateStruct {
        bytes32 description;
        mapping(bytes32 => bytes32) sub_state;
    }

    struct ObjectStruct {
        StateStruct state;
        address owner; 
        bool isObject;
    }

    mapping(bytes32 => ObjectStruct) objectStructs;
    bytes32[] public objectList;

    event LogNewObject(address sender, bytes32 indexed id, bytes32 sub_states_types, bytes32 sub_states_values, address owner);
    event LogChangeObjectState(address sender, bytes32 indexed id, bytes32 sub_states_types, bytes32 sub_states_values);
    event LogChangeObjectOwner(address sender, bytes32 indexed id, address newOwner);

    function isObject(bytes32 _id) public view returns(bool isIndeed) {
        return objectStructs[_id].isObject;
    }

    function getObjectCount() public view returns(uint count) {
        return objectList.length;
    }

    /*function setArraySize(uint256 _number_of_sub_states) public {

        number_of_sub_states = _number_of_sub_states;

    }

    function getArraySize() view public returns (uint256) {
       return number_of_sub_states;
    }*/

    function newObject(bytes32 _id, uint256 number_of_sub_states, bytes32[10] sub_states_types, bytes32[10] sub_states_values, address _owner) public returns(bool success) {
        require(!isObject(_id));

        uint256 counter=0;
        for(counter; counter < number_of_sub_states; counter++) {

            objectStructs[_id].state.sub_state[sub_states_types[counter]] = sub_states_values[counter];

            emit LogNewObject(msg.sender, _id, bytes32(sub_states_types[counter]), bytes32(sub_states_values[counter]), _owner);

        }

        objectStructs[_id].owner = _owner;
        objectStructs[_id].isObject = true;

        objectList.push(_id);

        return true;
    }

    function changeObjectState(bytes32 _id, uint256 number_of_sub_states, bytes32[10] sub_states_types, bytes32[10] sub_states_values) public returns(bool success) {
        require(isObject(_id));
        uint256 counter=0;
        for(counter; counter < number_of_sub_states; counter++) {

            objectStructs[_id].state.sub_state[sub_states_types[counter]] = sub_states_values[counter];

            emit LogChangeObjectState(msg.sender, _id, bytes32(sub_states_types[counter]), bytes32(sub_states_values[counter]));

        }
        //objectStructs[_id].state = StateStruct(_newState);
        //emit LogChangeObjectState(msg.sender, _id, _newState);
        return true;
    }

    function changeObjectOwner(bytes32 _id, address _newOwner) public returns(bool success) {
        require(isObject(_id));
        objectStructs[_id].owner = _newOwner;
        emit LogChangeObjectOwner(msg.sender, _id, _newOwner);
        return true;
    }

}

Whate is the reason ?

1 Answer 1

0

From the JavaScript API documentation

var str = web3.toAscii("0x657468657265756d000000000000000000000000000000000000000000000000");
console.log(str); // "ethereum"

what is that .input about? you just need to put this in quotes, it takes this hexstring. you can type convert like you did. What are you trying to do with that .input?

4
  • I edited my question. I tried "JavaScript API documentation" too, however same error.
    – Questioner
    Commented May 7, 2018 at 8:59
  • can you paste your smart contract too?
    – kjroger94
    Commented May 7, 2018 at 10:15
  • are you writing a dapp to invoke this or are you using the JS console after geth attach?
    – kjroger94
    Commented May 7, 2018 at 10:25
  • I deployed this contract on Tobalaba blockchain (a blockchain on Ethereum) tobalaba.etherscan.com and then using Parity I run it and invoke my functions using web3js commands. If you have any other question, please let me know. Thanks.
    – Questioner
    Commented May 7, 2018 at 10:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.