当我在 Node.js 控制台中运行以下命令时,出现以下错误(我在 Windows 10 上使用 web3 0.20.0):
web3.eth.getAccounts().then(e => console.log(e));
错误如下:
TypeError: Cannot read property 'then' of undefined
TypeError: callback is not a function
at c:\Users\Salam Khanji\Ether\node_modules\web3\lib\web3\property.js:119:13
at c:\Users\Salam Khanji\Ether\node_modules\web3\lib\web3\requestmanager.js:89:9
at XMLHttpRequest.request.onreadystatechange (c:\Users\Salam Khanji\Ether\node_modules\web3\lib\web3\httpprovider.js:119:7)
at XMLHttpRequestEventTarget.dispatchEvent (c:\Users\Salam Khanji\Ether\node_modules\web3\node_modules\xhr2\lib\xhr2.js:64:18)
at XMLHttpRequest._setReadyState (c:\Users\Salam Khanji\Ether\node_modules\web3\node_modules\xhr2\lib\xhr2.js:354:12)
at XMLHttpRequest._onHttpResponseEnd (c:\Users\Salam Khanji\Ether\node_modules\web3\node_modules\xhr2\lib\xhr2.js:509:12)
最佳答案
我猜他们可能是以下问题之一
- 较旧版本的 web3js,可能不支持 Promise
- 检查你的geth命令,你可能没有启用rpc,要在web3js中访问geth,那么你需要启用。如果你没有添加 --rpc 你就无法访问
geth --rpc --rpccorsdomain "*" --rpcapi eth,net,web3,personal,miner,txpool,debug --datadir ~/Desktop/local-network/
- 如果您想了解所有帐户,请尝试将您的行替换为以下行。
var account = web3.eth.accounts;//将返回所有帐户。
关于node.js - 以太坊 Node 中的 web3(然后是属性),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48608590/