Web3js
web3.eth.personal :“找不到方法”
當我呼叫 web3.eth.personal 的方法時,我收到如下錯誤:
> web3.eth.personal.getAccounts().then(console.log); Promise { <pending>, domain: Domain { domain: null, _events: { removeListener: [Function: updateExceptionCapture], newListener: [Function: updateExceptionCapture], error: [Function: debugDomainError] }, _eventsCount: 3, _maxListeners: undefined, members: [] } } > (node:34481) UnhandledPromiseRejectionWarning: Error: Returned error: Method not found at Object.ErrorResponse (/home/s/node_modules/web3-core-helpers/src/errors.js:29:16) at /home/s/node_modules/web3-core-requestmanager/src/index.js:140:36 at XMLHttpRequest.request.onreadystatechange (/home/s/node_modules/web3-providers-http/src/index.js:77:13) at XMLHttpRequestEventTarget.dispatchEvent (/home/s/node_modules/xhr2/lib/xhr2.js:64:18) at XMLHttpRequest._setReadyState (/home/s/node_modules/xhr2/lib/xhr2.js:354:12) at XMLHttpRequest._onHttpResponseEnd (/home/s/node_modules/xhr2/lib/xhr2.js:509:12) at IncomingMessage.<anonymous> (/home/s/node_modules/xhr2/lib/xhr2.js:469:24) at IncomingMessage.emit (events.js:185:15) at IncomingMessage.emit (domain.js:440:23) at endReadableNT (_stream_readable.js:1106:12) at process._tickCallback (internal/process/next_tick.js:178:19) (node:34481) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1) (node:34481) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
我是否需要安裝或定義任何其他要求?我還安裝了 web3-eth-personal,如下所示:
npm install web3-eth-personal
然後我使用以下命令:
var Personal = require('web3-eth-personal'); var personal = new Personal( new Personal.providers.HttpProvider("http://localhost:8545")); web3.eth.personal.getAccounts().then(console.log);
PS。我找到了解決方案:我開始使用 Parity,
Parity --chain tobalaba --jsonrpc-hosts all --jsonrpc-interface all --jsonrpc-cors '*' --jsonrpc-apis 'web3,eth,net,parity,traces,rpc,parity_set,personal'
現在它可以工作了。
對於 parity:如選項中所述,在 parity –help 下可用,並非所有 API 預設都公開。但是,您可以通過使用標誌執行奇偶校驗來簡單地啟用它們:
--jsonrpc-apis APIS
exp :parity --jsonrpc-apis web3,rpc,personal,parity_accounts,eth,net,parity,parity_set,signer
為 geth 開始你的 geth
--rpcapi "eth,net,web3,personal"
我
web3.personal
直接使用,我能夠再次訪問其中的方法。這很奇怪,因為在控制台中,
web3.eth.personal
它仍然有效,但在瀏覽器的腳本中,它沒有我希望這有幫助。