6

关于web3.js 1.0版本中的事件监听问题

 3 years ago
source link: https://learnblockchain.cn/question/1766
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
关于web3.js 1.0版本中的事件监听问题

关于web3.js 1.0版本中的事件监听问题

我在使用 web3.js 1.0 版本的过程中,虽然严格对照文档,仍然发现事件监听无法执行,对整个 1.0 版本还比较迷惑。

编译工具是 vscode。

问题 1:引入 web3 的时候,还需要 var Web3 = require("web3")吗?疑似不用了。我在 head 里用 script 引入了 cdn 的 web3 链接,执行的时候调用合约方法都没有问题;反而是 var Web3 = require("web3")的时候,控制台告诉我 require is not a function

问题 2:惟独在搞事件监听的时候失败了,这块和 0.2.0 版本似乎有巨大的差异,我的 solidity 代码里事件名为 reward,JS 代码里写成:

myContract.events.reward ( function(error, event){
if(!error) {
console.log(event);
}
});

结果啥也没执行出来。调试了很久,十分迷惑,望得到大大的解答。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK