admin管理员组

文章数量:1410688

When I called the receivepol1 contract function on the front-end webpage, the event text content of 'Transaction Successful111111: ' was not displayed on the front-end webpage. Why is this? Didn't the "Index" event in the contract get a response? But I checked the transaction hash information and found that the index event in the contract had been triggered, but why was the text content not displayed on the front-end webpage?

the solidity contract code as follows:

// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.4;
import "@chainlink/contracts/src/v0.8/automation/interfaces/KeeperCompatibleInterface.sol";
contract Biao {
    event Index(uint256 indexed amount1);
    address payable public myself;
    constructor() {
        myself = payable(msg.sender);
    }
    function receivepol1() public payable returns (uint256){
        uint256 amount1 = 2;
        emit Index(amount1);
        return amount1;
    }
}

the javascript code as follows:

contract = new web3.eth.Contract(abi, contractAddress);
contract.events.Index({}, async function(error, event) {
  if (!error){
      console.log('Event detected:', event); 
      indexvalue = event.returnValues.amount1;
      var text = document.createElement('span');
      text.innerText = 'Transaction Successful111111: '; 
      text.style.position = 'absolute';
      text.style.top = '690px';
      text.style.left = '500px';
      text.style.color = 'black';
      text.style.fontSize = '14px';
      text.style.fontWeight = 'bold';
      text.style.backgroundColor = 'rgba(0,0,0,0.5)'; 
      text.style.padding = '5px'; 
      document.body.appendChild(text);
  } else {
      console.error('Error listening to Debug events:', error);
  }
});

the html code as follows:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>My DApp</title>
    <link rel="icon" href="aa.png" type="image/png">
</head>
<body>
    <h1>Welcome to My DApp</h1>
    <script src=".3.0/web3.min.js"></script>
    <script src="app.js"></script>
</body>
</html>

Why is there no text information displayed on the front end of the web page? Is it because the code in the javascript is incorrect?

本文标签: Unable to respond to events in a contract using the web3js library in javascript codeStack Overflow