1:通过IpcProvider来连接节点得到的web3实例,是不支持同步操作的,比如调用web3.eth.accounts会发生如下错误:
/Users/guanhongchang/Documents/区块链学习/node/nodeweb3/node_modules/web3/lib/web3/ipcprovider.js:192
throw new Error('You tried to send "'+ payload.method +'" synchronously. Synchronous requests are not supported by the IPC provider.');
^
Error: You tried to send "eth_accounts" synchronously. Synchronous requests are not supported by the IPC provider.
at IpcProvider.send (/Users/guanhongchang/Documents/区块链学习/node/nodeweb3/node_modules/web3/lib/web3/ipcprovider.js:192:15)
at RequestManager.send (/Users/guanhongchang/Documents/区块链学习/node/nodeweb3/node_modules/web3/lib/web3/requestmanager.js:58:32)
at Eth.get [as accounts] (/Users/guanhongchang/Documents/区块链学习/node/nodeweb3/node_modules/web3/lib/web3/property.js:107:62)
at Object.<anonymous> (/Users/guanhongchang/Documents/区块链学习/node/nodeweb3/app.js:16:24)
at Module._compile (module.js:660:30)
at Object.Module._extensions..js (module.js:671:10)
at Module.load (module.js:573:32)
at tryModuleLoad (module.js:513:12)
at Function.Module._load (module.js:505:3)
at Function.Module.runMain (module.js:701:10)
将对应的语句改为异步请求就可以了,例如将web3.eth.accounts改为:
web3.eth.getAccounts(function(error, result){
})
就可以解决问题了
2:web3.eth.getBlock这个方法比如要传入一个参数才可以,不然会报如下错:
Error: invalid argument 0: hex string without 0x prefix
at Object.InvalidResponse (/Users/guanhongchang/Documents/区块链学习/node/nodeweb3/node_modules/web3/lib/web3/errors.js:38:16)
at RequestManager.send (/Users/guanhongchang/Documents/区块链学习/node/nodeweb3/node_modules/web3/lib/web3/requestmanager.js:61:22)
at Eth.send [as getBlock] (/Users/guanhongchang/Documents/区块链学习/node/nodeweb3/node_modules/web3/lib/web3/method.js:145:58)
at Object.<anonymous> (/Users/guanhongchang/Documents/区块链学习/node/nodeweb3/app.js:30:23)
at Module._compile (module.js:660:30)
at Object.Module._extensions..js (module.js:671:10)
at Module.load (module.js:573:32)
at tryModuleLoad (module.js:513:12)
at Function.Module._load (module.js:505:3)
at Function.Module.runMain (module.js:701:10)
可以传入的参数有:区块序列号、哈希值、字符串(earliest、latest、pending)
3: TypeError: web3.isConnected is not a function
在js中调用web3.isConnected()会出现以上错误,版本问题,1.0之后这个方法没了
指定安装web3的版本为1.0之前的,例如执行
npm install web3@^0.20.0
来安装0.20版本就可以解决了上面的问题