【问题标题】:`Web3.utils` is undefined and `Web3.eth` is undefined`Web3.utils` 未定义,`Web3.eth` 未定义
【发布时间】:2019-09-01 06:36:08
【问题描述】:

将 Web3 从 1.0.0-beta.34 升级到 1.0.0-beta.37 后,尝试访问 Web3.utilsWeb3.ethWeb3.bzzWeb3.shh 的任何属性都会出错。例如,在2_deploy_contract.js 调用web3.utils.utf8ToHex('some string') 如下:

const Web3 = require('web3');
web3.utils.utf8ToHex('some string');

正在引发以下异常:

> TypeError: Cannot read property 'utf8ToHex' of undefined

我发现下面的代码sn-p:

const Web3 = require('web3');
console.log("Web3.eth = " + Web3.eth)
console.log("Web3.utils = " + Web3.utils)
console.log("Web3.bzz = " + Web3.bzz)
console.log("Web3.shh = " + Web3.shh)

将输出:

> Web3.eth = undefined 
> Web3.utils = undefined 
> Web3.bzz = undefined
> Web3.shh = undefined

【问题讨论】:

    标签: web3


    【解决方案1】:

    静态属性(Web3.utilsWeb3.eth 等...)已被删除,因为如果有人只使用utils,那么他可能应该直接使用web3-utilsweb3-eth 模块,而不是捆绑完整的库 (ref)。

    示例工作代码:

    const Web3Utils = require('web3-utils');
    const someString = Web3Utils.utf8ToHex('some string');
    

    注意:长期目标是移除 Web3 类并创建 Web3 命名空间。但这将在稳定版发布后完成 (ref)。

    【讨论】:

    • 既然可以有3个包,为什么还要一个包
    猜你喜欢
    • 2021-09-15
    • 2018-12-05
    • 2018-11-04
    • 2019-12-13
    • 2019-07-23
    • 1970-01-01
    • 2022-07-21
    • 2020-01-23
    • 2022-01-15
    相关资源
    最近更新 更多