Solidity

TypeError:market.FileViewed 在嘗試觀看事件時不是函式

  • October 17, 2019

我想觀看 fileViewed 事件,它會TypeError: market.FileViewed is not a function拋出

marketplace.FileViewed{
          ^

我的堅固契約:

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);
   }

}

這是我的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})
    })
 }

FileViewed如果您使用的是 web3 v1.2,則這是一個事件,您必須像這樣使用:

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

有關更多詳細資訊,請參閱:https ://web3js.readthedocs.io/en/v1.2.1/web3-eth-contract.html#contract-events

引用自:https://ethereum.stackexchange.com/questions/76713