0

I want to watch for fileViewed event and it throws TypeError: market.FileViewed is not a function in

marketplace.FileViewed{
           ^

My solidity contract:

pragma solidity ^0.5.0;

contract Marketplace {
    string public name;
    address owner;
    uint public fileCount = 0;
    mapping(uint => File) public files; 


    struct File
    {
        uint id;
        string name;
        address owner;
        address sharedWith;
    }

    mapping(uint => Product) public products;

    struct Product {
        uint id;
        string name;
        address owner;
        address sharedWith;
    }

    event ProductCreated(
        uint id,
        string name,
        address owner,
        address sharedWith
    );

    event FileViewed(
        uint id,
        string name,
        address owner,
        address sharedWith
    );


    constructor() public {
        name = "File Sharing System";
        owner = msg.sender;
    }

    modifier onlyOwner()
    {
        require(owner==msg.sender);
        _;
    }

    function addFile(string memory _name, address _address) onlyOwner public
    {
        fileCount++;
        products[fileCount] = Product(fileCount,_name,owner,_address);
        emit ProductCreated(fileCount, _name, owner, _address);
    }

    function viewFile(uint _id) public view
    {
        Product memory _file = products[_id];
        require(msg.sender==_file.sharedWith,"Not shared with you!!");
        emit FileViewed(fileCount, _name, owner, _address);
    }

}

This is my js:

marketplace.FileViewed(
    function(){
      if(this.state.account!=results.args.sharedWith)
      {
        window.alert("File not shared with you")
      }
      else
      {
        window.alert("File viewed")
      }
    })

  viewFile(id)
  {
    this.setState({loading: true})
    this.state.marketplace.methods.viewFile(id).send({from: this.state.account })
     .once('receipt', (receipt) => {
        this.setState({loading: false})
     })
  }

1 Answer 1

0

FileViewed is an event if you are using web3 v1.2 you have to use like this:

marketplace.events.FileViewed({
    fromBlock: 0
}, function (error, event) {
    if (error) {
        console.log("Error: ", error);
    } else {
        console.log("File viewed: ", event);
    }
})

For more details see this: https://web3js.readthedocs.io/en/v1.2.1/web3-eth-contract.html#contract-events

8
  • Even after this, the program is not printing anything and now the error is gone though, thanks for that. Can you help me with that?
    – vteja711
    Oct 21, 2019 at 10:03
  • Also please upvote the question, if you think this was a valid question. Thanks
    – vteja711
    Oct 21, 2019 at 10:07
  • @vteja711 I'd check if your contract is generating the event. In any case probably is better to create a separate question. A few thing to verify: How do you connect to the network? Are other events being generated?
    – Ismael
    Oct 21, 2019 at 15:04
  • I am using ganache as my local network and metamask for my connections and interactions
    – vteja711
    Oct 22, 2019 at 1:48
  • @vteja711 The problem is that ´viewFile´ is marked as ´view´. That implies no transaction will be created when it is invoked. So no events are being generated. Then compiler should throw an error when using an event inside a view function.
    – Ismael
    Oct 22, 2019 at 4:58

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.