current position:Home>About Web3 Event listening in JS version 1.0

About Web3 Event listening in JS version 1.0

2022-02-03 23:37:41 Q & A of Denglian community

I am using web3.js 1.0 During version , Although strictly against the documents , It is still found that event listening cannot be performed , To the whole 1.0 The version is still confused .

The compiler tool is vscode.

problem 1: introduce web3 When , It also needs to be var Web3 = require("web3") Do you ? I don't think so . I am here head In the use script Introduced cdn Of web3 link , There is no problem calling contract methods during execution ; Instead, it's var Web3 = require("web3") When , The console told me require is not a function

problem 2: Only in the event of monitoring failed , This and 0.2.0 There seems to be a huge difference between the versions , my solidity The event in the code is called reward,JS The code is written as : ( function(error, event){if(!error) {console.log(event);}});

Nothing came out . Debugging for a long time , Very confused , Hope to get a big answer .

Take the answer 1:
  1. javascript In a standard environment , I won't support it require, Need to be in node.js The project supports .

  2. Need to listen for events , The link protocol with the node needs to be websocket, That is to use such as : ws://localhost:7545, instead of http://localhost:7545. Please refer to this article :

copyright notice
author[Q & A of Denglian community],Please bring the original link to reprint, thank you.

Random recommended