node.js - 以太坊 Node 中的 web3(然后是属性)

标签 node.js ethereum web3js

当我在 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)

最佳答案

我猜他们可能是以下问题之一

  1. 较旧版本的 web3js,可能不支持 Promise
  2. 检查你的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/

    相关文章:

    用于验证逗号分隔字符串的 JavaScript 正则表达式

    javascript - 在 For 语句中使用 Query 时如何解决响应问题

    blockchain - 如何使用 Solidity 和 Web.js 在以太坊区 block 链上保存和检索数据

    javascript - 无法将 Solidity 合约部署到 Rinkeby 网络(无效的 asm.js : Invalid member of stdlib)

    token - 通过合约发送代币时出现 SafeMath 错误

    node.js - 发送以太坊交易,Web3.js没有源地址,但有源私钥?

    node.js - 错误 : The method eth_sendTransaction does not exist/is not available

    javascript - 引用错误 : firebase is not defined in firebase with nodejs

    javascript - 从 https 网站调用 http api

    go - 如何理解core/types/block.go中的 'rlpHash'方法