javascript - CryptoCurrency NODE.js Web3 Ethereum - 无法连接到 TESTRPC

标签 javascript node.js ethereum web3js

我正在尝试从我的 web3 应用程序连接到我的 testrpc 实例,但我似乎无法连接。下面的代码应该返回 testrpc 链中所有帐户的数组。

代码:

Web3 = require('web3')
web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
console.log(web3.eth.accounts);

它返回下面的内容,在对象intelliJ中,currentprovider对象如下。 enter image description here

它似乎只是打印了我认为的对象:

Accounts {
  eth: 
   Eth {
     currentProvider: [Getter/Setter],
     _requestManager: RequestManager { provider: [Object], providers: [Object], subscriptions: {} },
     givenProvider: null,
     providers: 
      { WebsocketProvider: [Function: WebsocketProvider],
        HttpProvider: [Function: HttpProvider],
        IpcProvider: [Function: IpcProvider] },
     _provider: HttpProvider { host: 'http://localhost:8545', timeout: 0, connected: false },
     setProvider: [Function],
     BatchRequest: [Function: bound Batch],
     extend: 
      { [Function: ex]
        formatters: [Object],
        utils: [Object],
        Method: [Function: Method] },
     clearSubscriptions: [Function],
     getProtocolVersion: { [Function: send] request: [Function: bound ], call: 'eth_protocolVersion' },
     getCoinbase: { [Function: send] request: [Function: bound ], call: 'eth_coinbase' },
     isMining: { [Function: send] request: [Function: bound ], call: 'eth_mining' },
     getHashrate: { [Function: send] request: [Function: bound ], call: 'eth_hashrate' },
     isSyncing: { [Function: send] request: [Function: bound ], call: 'eth_syncing' },
     getGasPrice: { [Function: send] request: [Function: bound ], call: 'eth_gasPrice' },
     getAccounts: { [Function: send] request: [Function: bound ], call: 'eth_accounts' },
     getBlockNumber: { [Function: send] request: [Function: bound ], call: 'eth_blockNumber' },
     getBalance: { [Function: send] request: [Function: bound ], call: 'eth_getBalance' },
     getStorageAt: { [Function: send] request: [Function: bound ], call: 'eth_getStorageAt' },
     getCode: { [Function: send] request: [Function: bound ], call: 'eth_getCode' },
     getBlock: { [Function: send] request: [Function: bound ], call: [Function: blockCall] },
     getUncle: { [Function: send] request: [Function: bound ], call: [Function: uncleCall] },
     getCompilers: { [Function: send] request: [Function: bound ], call: 'eth_getCompilers' },
     getBlockTransactionCount: 
      { [Function: send]
        request: [Function: bound ],
        call: [Function: getBlockTransactionCountCall] },
     getBlockUncleCount: 
      { [Function: send]
        request: [Function: bound ],
        call: [Function: uncleCountCall] },
     getTransaction: 
      { [Function: send]
        request: [Function: bound ],
        call: 'eth_getTransactionByHash' },
     getTransactionFromBlock: 
      { [Function: send]
        request: [Function: bound ],
        call: [Function: transactionFromBlockCall] },
     getTransactionReceipt: 
      { [Function: send]
        request: [Function: bound ],
        call: 'eth_getTransactionReceipt' },
     getTransactionCount: { [Function: send] request: [Function: bound ], call: 'eth_getTransactionCount' },
     call: { [Function: send] request: [Function: bound ], call: 'eth_call' },
     estimateGas: { [Function: send] request: [Function: bound ], call: 'eth_estimateGas' },
     sendSignedTransaction: { [Function: send] request: [Function: bound ], call: 'eth_sendRawTransaction' },
     signTransaction: { [Function: send] request: [Function: bound ], call: 'eth_signTransaction' },
     sendTransaction: { [Function: send] request: [Function: bound ], call: 'eth_sendTransaction' },
     sign: { [Function: send] request: [Function: bound ], call: 'eth_sign' },
     compile: { solidity: [Object], lll: [Object], serpent: [Object] },
     submitWork: { [Function: send] request: [Function: bound ], call: 'eth_submitWork' },
     getWork: { [Function: send] request: [Function: bound ], call: 'eth_getWork' },
     getPastLogs: { [Function: send] request: [Function: bound ], call: 'eth_getLogs' },
     subscribe: { [Function] call: undefined },
     net: 
      Net {
        currentProvider: [Getter/Setter],
        _requestManager: [Object],
        givenProvider: null,
        providers: [Object],
        _provider: [Object],
        setProvider: [Function],
        BatchRequest: [Function: bound Batch],
        extend: [Object],
        getId: [Object],
        isListening: [Object],
        getPeerCount: [Object],
        getNetworkType: [Function: bound getNetworkType] },
     accounts: [Circular],
     personal: 
      Personal {
        currentProvider: [Getter/Setter],
        _requestManager: [Object],
        givenProvider: null,
        providers: [Object],
        _provider: [Object],
        setProvider: [Function],
        BatchRequest: [Function: bound Batch],
        extend: [Object],
        getAccounts: [Object],
        newAccount: [Object],
        unlockAccount: [Object],
        sendTransaction: [Object],
        lockAccount: [Object],
        importRawKey: [Object],
        sign: [Object],
        ecRecover: [Object],
        net: [Object] },
     Contract: [Function: Contract],
     Iban: 
      { [Function: Iban]
        toAddress: [Function],
        toIban: [Function],
        fromAddress: [Function],
        fromBban: [Function],
        createIndirect: [Function],
        isValid: [Function] },
     abi: ABICoder { _types: [Array] } },
  wallet: 
   Wallet {
     length: 0,
     _accounts: [Circular],
     defaultKeyName: 'web3js_wallet' } }

testRPC 服务器也成功启动: enter image description here

最佳答案

您的连接正常。

要返回帐户数组,请使用 web3.eth.getAccounts() 而不是 web3.eth.accounts

上面的代码应该看起来像......

Web3 = require('web3')
web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
web3.eth.getAccounts().then(accounts => console.log(accounts));

给你一些类似......的东西

[ '0x09e67e758381Fb878b9B1dCB91f2D5a62E00035a',
  '0x1BaABCa410a88564aC2BD9b8570D21b6e898C4C3',
  '0x8bfF74332C52065d2CC46f96c191aCDeec1AFEF2',
  '0x19070EaC7D68a710A5843Ee8cA3de258024da3Cb',
  '0x909394eA6feA0aDba05f093Ae43782e42b462F56',
  '0x63527Ef51b17830d6F597686C366A1995fd31bb2',
  '0xf476D31fAD8605d9D9D7b557C5117Bbe102935bc',
  '0xA64ba4e9d332106181A073E6eD20F876D6e28856',
  '0x126533F5FC78E355Fc35476Ae1392D338366D229',
  '0x291c17c0901FC9143D0DC43E1455E20D26342377' ]

关于javascript - CryptoCurrency NODE.js Web3 Ethereum - 无法连接到 TESTRPC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45253936/

相关文章:

javascript - 在 D3 中设计图例

node.js - Node js mysqlquery 尝试添加 .then

android - 智能合约包装器的交易哈希和超时异常

javascript - 在 ethers.js 中设置合约方法的气体限制

javascript - 如何在 grandCouncil 数组中获取正确的对象变量名称?

javascript - Fabric 3.4.0 文本格式

javascript - jQuery 不工作

javascript - onSubmit onClick 的 nextjs 事件处理程序不起作用

node.js - Sequelize : Change column type to ENUM

ethereum - 使用 web3js 从交易哈希中获取 token 传输详细信息