首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >web3.js:错误: Web3.js上不存在/不可用的方法shh_newSymKey

web3.js:错误: Web3.js上不存在/不可用的方法shh_newSymKey
EN

Ethereum用户
提问于 2018-06-19 13:26:52
回答 1查看 1K关注 0票数 3

我的主要目标是使用RPC APIWeb3.pyWeb3.js调用D1函数。当我使用web3.shh时,我可以调用geth attach函数;这是我最后的选择。

我正在跟踪这个回答

  • geth版本:1.8.0-unstable
  • 我正在使用geth标志和--rpcapi "admin,eth,net,web3,debug,shh"运行--shh
  • console.log(web3.version);返回: api:'0.20.5'

当我运行以下脚本时:

代码语言:javascript
复制
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'中给出了以下错误:

代码语言:javascript
复制
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. (/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)

Please注意,我已经尝试过使用 web3@1.0.0-beta.34 that也给出了类似于相同错误的错误:

代码语言:javascript
复制
Promise {  }
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. (/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.

<#>如何修复此错误?我做错什么了?

请注意,可用的shh函数可以看到这里console.log(web3.shh)的输出。

EN

回答 1

Ethereum用户

回答已采纳

发布于 2018-06-25 14:21:52

newSymKey();函数返回一个承诺(需要解析)以获得返回的值。

这就是为什么版本web3@1.0.0-beta.34中的错误为您提供了带有UnhandledPromiseRejectionWarningpromise pending消息,而在web3@0.20.5版本中,错误状态为shh_newSymKey does not exist/is not available

所以试试var kId = web3.shh.newSymKey().then(console.log);

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

我更喜欢使用更新的异步/等待函数,而不是承诺(读起来更容易):

代码语言:javascript
复制
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();
票数 1
EN
页面原文内容由Ethereum提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://ethereum.stackexchange.com/questions/51585

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档