Go-Ethereum

web3.js:錯誤:方法 shh_newSymKey 不存在/在 Web3.js 上不可用

  • June 25, 2018

我的主要目標是使用,或呼叫web3.shh函式。我可以在使用時呼叫函式;這是我最後的選擇。RPC API``Web3.py``Web3.js``web3.shh``geth attach

我正在關注這個答案

  • geth版本:1.8.0-unstable
  • geth帶著--shh旗幟奔跑--rpcapi "admin,eth,net,web3,debug,shh"
  • console.log(web3.version);返回:api '0.20.5':。

當我執行以下腳本時:

Web3 = require("web3");
web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));

if(!web3.isConnected()){ //web3@0.20.5
//if(!web3.eth.net.isListening().then(console.log)){ //web3@1.0.0-beta.34
   console.log("notconnected");
   process.exit();
}

var kId = web3.shh.newSymKey(); //Error occurs. 

var kId = web3.shh.newSymKey();給出以下錯誤web3@'0.20.5'

Error: The method shh_newSymKey does not exist/is not available
   at Object.InvalidResponse (/home/alper/eBlocBroker/node_modules/web3/lib/web3/errors.js:38:16)
   at RequestManager.send (/home/alper/eBlocBroker/node_modules/web3/lib/web3/requestmanager.js:61:22)
   at Shh.send [as newSymKey] (/home/alper/eBlocBroker/node_modules/web3/lib/web3/method.js:145:58)
   at Object.<anonymous> (/home/alper/eBlocBroker/dd.js:9:20)
   at Module._compile (module.js:649:30)
   at Object.Module._extensions..js (module.js:660:10)
   at Module.load (module.js:561:32)
   at tryModuleLoad (module.js:501:12)
   at Function.Module._load (module.js:493:3)
   at Function.Module.runMain (module.js:690:10)

請注意,我已經嘗試過,它web3@1.0.0-beta.34也給出了這個錯誤,看起來像同樣的錯誤:

Promise { <pending> }
true
(node:16162) UnhandledPromiseRejectionWarning: Error: Returned error: The method shh_newSymKey does not exist/is not available
   at Object.ErrorResponse (/home/alper/eBlocBroker/node_modules/web3-shh/node_modules/web3-core-helpers/src/errors.js:29:16)
   at /home/alper/eBlocBroker/node_modules/web3-shh/node_modules/web3-core-requestmanager/src/index.js:140:36
   at XMLHttpRequest.request.onreadystatechange (/home/alper/eBlocBroker/node_modules/web3/node_modules/web3-providers-http/src/index.js:77:13)
   at XMLHttpRequestEventTarget.dispatchEvent (/home/alper/eBlocBroker/node_modules/xhr2/lib/xhr2.js:64:18)
   at XMLHttpRequest._setReadyState (/home/alper/eBlocBroker/node_modules/xhr2/lib/xhr2.js:354:12)
   at XMLHttpRequest._onHttpResponseEnd (/home/alper/eBlocBroker/node_modules/xhr2/lib/xhr2.js:509:12)
   at IncomingMessage.<anonymous> (/home/alper/eBlocBroker/node_modules/xhr2/lib/xhr2.js:469:24)
   at IncomingMessage.emit (events.js:185:15)
   at endReadableNT (_stream_readable.js:1101:12)
   at process._tickCallback (internal/process/next_tick.js:114:19)
(node:16162) 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:16162) [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.

**$$ Q $$**我該如何解決這個錯誤?我做錯了什麼?

請注意這裡shh可以看到可用的功能;的輸出。console.log(web3.shh)

newSymKey();函式返回一個需要解析的promise,以獲取返回值。

這就是為什麼版本中的錯誤web3@1.0.0-beta.34會給你一個promise pending帶有 的消息UnhandledPromiseRejectionWarning,並且在web3@0.20.5版本中,錯誤狀態是shh_newSymKey does not exist/is not available

$$ yet $$ 所以試試var kId = web3.shh.newSymKey().then(console.log);

或者var kId = web3.shh.newSymKey().then(function(result) { console.log(result) //will log results. })

我更喜歡使用較新的 async / await 函式而不是 promises(更容易閱讀):

Web3 = require("web3");
web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));     

async function connect() { //note async function declaration
   if(!await web3.isConnected()){ //await web3@0.20.5
   //if(!await web3.eth.net.isListening()){ //await web3@1.0.0-beta.34
       console.log("notconnected");
       process.exit();
   }

   var kId = await web3.shh.newSymKey(); //note await
   console.log(kId);

}

connect();

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