Web3js

在 truffle 5 控制台中檢索特定帳戶

  • April 26, 2019

我正在關註一個松露教程,其中個人使用者web3.eth.accounts從 Ganash 檢索所有帳戶。但是,這似乎已貶值,並複制了我使用的結果web3.eth.getAccounts(),如下所示。在此處輸入圖像描述

然而,在 truffle 控制台web3.eth.accounts[0]中執行下一個命令,該命令在位置 1(以及第一個帳戶)處顯示帳戶。

最終,這是我的問題,因為我還無法弄清楚如何在 truffle 5 中完成同樣的任務。我已經嘗試過web3.eth.getAccounts([0])並且web3.eth.getAccounts().then(e => let firstAcc=e[0]; console.log(firstAcc));有人建議過,但都不起作用。感謝我能得到的任何幫助或指導,以幫助我解決這個小問題。

問候

Truffle v5 使用 web3 v1,它使用 Promise 來返回值。要訪問第三個帳戶,您必須執行以下操作:

account = (await web3.eth.getAccounts())[3]

現在account包含地址。

您應該嘗試按照 Ismael 的建議進行操作。但是,如果您只嘗試 account = (await web3.eth.getAccounts()),這將在這樣的兩個 cmd 命令中起作用

$$ 3 $$. 這會給你 undefined 。你應該這樣嘗試 松露(開發)>帳戶=(等待web3.eth.getAccounts())

$$ 3 $$ 不明確的

松露(開發)> 帳戶

引用自:https://ethereum.stackexchange.com/questions/64924