0

Hey I am new at blockchain development and I need to make contract name organization that create sessions contracts. I had already implemented the solidity code but I have a problem in front-end development. I can't call session contract methods. It give me this error

Uncaught (in promise) TypeError: r is not a function

My Solidity Code:-

pragma solidity 0.5.0;

contract Organization {

     address creator;
     event sessionnCreated(string name,address sessionAddress ,address creator);

       modifier onlyCreator(){
        require(msg.sender == creator);
        _;
    } 
     function createdSession(
      string memory _sessionName,
      string memory _description,
      uint _startTime,
      uint _endTime,
      address[] memory _lecturer,
      address[] memory _attendes
     ) public   returns(address) {

        Session sessionAddress = new Session(_sessionName , _description , _startTime , _endTime, _lecturer,_attendes );
        emit sessionnCreated(_sessionName,address(sessionAddress),creator);
        return address(sessionAddress);
     }

}

//Session

contract Session {

      address public creator;  // The address of parent Organization

      string sessionName;
      string description;
      uint startTime;
      uint endTime;
      address[] lecturer;
      address[] attendes; 

      int[] result; 
      mapping(address => int) public attendes_feedback; //institution boardMembers
      modifier onTime(uint _startTime){
            require(now < _startTime);
            _;
      }
      constructor (string memory _sessionName, string memory _description, uint _startTime,uint _endTime,address[] memory   _lecturer,address[] memory  _attendes) public onTime(_startTime){

              sessionName =  _sessionName;
              description = _description;
              startTime = _startTime;
              endTime = _endTime;
              attendes = _attendes;
              lecturer = _lecturer;
              initAttendes(attendes);
      } 

    function initAttendes(address[] memory _attendes) private{
           for(uint i=0 ; i < _attendes.length ; i++){
            attendes_feedback[_attendes[i]] = -1;
        }
    }

    function Time() public view returns (bool){
       return (now >=  startTime  && now <= endTime);          
     }

    modifier checkTime(){
        require(Time());
        _;
      }
    function take_feedback(address _voter,uint8 _feedback)  public checkTime {
          require(attendes_feedback[_voter] != 0);
          attendes_feedback[_voter] = _feedback;
          result[_feedback]++;
    }

  function seeResult() public view returns(int[] memory){
          return result;
  }
 }

My js code:-

import Web3 from "web3";
import OrganizationArtifact from "../../build/contracts/Organization.json";

const App = {
    web3: null,
    account: null,
    meta: null,

    start: async function() {
        const { web3 } = this;

        try {
            // get contract instance
            const networkId = await web3.eth.net.getId();
            const deployedNetwork = OrganizationArtifact.networks[networkId];
            this.meta = new web3.eth.Contract(
                OrganizationArtifact.abi,
                deployedNetwork.address,
            );

            // get accounts
            const accounts = await web3.eth.getAccounts();
            this.account = accounts[0];

        } catch (error) {
            console.error("Could not connect to contract or chain.");
        }
    },

    //Create session
    createdSession: async function(_sessionName, _description, _startTime, _endTime, _lecturer, _attendes) {
        var contractAddress;
        var address;
        const { createdSession } = this.meta.methods;
        contractAddress = await createdSession(_sessionName, _description, _startTime, _endTime, _lecturer, _attendes).send({ from: this.account });
        var address = await createdSession(_sessionName, _description, _startTime, _endTime, _lecturer, _attendes).call();
        console.log(address);
        alert("Contract created successfully contact addresss is " + address);
    },
  //Take Feedback
    takeVote: async function() {
        var _sessionName = $('#feedback_session_name').val();
        var _feedback = $('#feedback').val();
        const { takeVote } = this.meta.methods;
        await takeVote(_sessionName, _feedback).send({ from: this.account });
        alert("done");
    },
window.App = App;

window.addEventListener("load", function() {
    if (window.ethereum) {
        // use MetaMask's provider
        App.web3 = new Web3(window.ethereum);
        window.ethereum.enable(); // get permission to access accounts
    } else {
        console.warn(
            "No web3 detected. Falling back to http://127.0.0.1:9545. You should remove this fallback when you deploy live",
        );
        // fallback - use your fallback strategy (local node / hosted node + in-dapp id mgmt / fail)
        App.web3 = new Web3(
            new Web3.providers.HttpProvider("http://127.0.0.1:9545"),
        );
    }

    App.start();
});

Create Session function in organization contract work well because it's in organization contract but take_feedback function isn't working at all(it give this error "TypeError: r is not a function" when I call it in js file ).

If anyone could help me how to access methods of the child contract? Thanks in advance

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.