简体   繁体   English

Web3未连线

[英]Web3 Not Wiring Up

I made a new project with truffle unbox react and assuming I deployed the dapp directly to Rinkeby with Infura I'm having a problem interacting with web3 in my app.js 我用松露解开盒反应做出了一个新项目,并假设我使用Infura将dapp直接部署到Rinkeby,但在我的app.js中与web3交互时遇到问题

I have a web3 file in my src folder here is its content 我的src文件夹中有一个web3文件,这是其内容

import Web3 from 'web3';

let web3 = new Promise(function(resolve, reject) {

window.addEventListener('load', function() {
 var results
 var web3 = window.web3

if (typeof web3 !== 'undefined') {
  web3 = new Web3(web3.currentProvider)

  results = {
    web3: web3
  }
  console.log(web3);
  console.log('Injected web3 detected.');

  resolve(results)
} else {
  var provider = new Web3.providers.HttpProvider('http://127.0.0.1:9545')

  web3 = new Web3(provider)

  results = {
    web3: web3
  }

  console.log('No web3 instance injected, using Local web3.');

  resolve(results)
  }
 })  
})

 export default web3

I then import that file to my app.js 然后,我将该文件导入到我的app.js中

import web3 from './web3';

And i just call in a JSX paragraph tag 我只是调用JSX段落标签

  <p>  web3.utils.fromWei(this.state.balance, 'ether')  </p>

The error I get in my console log is as follows: "Uncaught TypeError: Cannot read property 'fromWei' of undefined" 我在控制台日志中遇到的错误如下:“未捕获的TypeError:无法读取未定义的属性'fromWei'”

I'm operating under the (possibly incorrect) assumption that web3 was downloaded when I ran the truffle unbox react command so no need to install it with npm. 我正在(可能是错误的)假设下运行了truffle unbox react命令时下载了web3,因此无需使用npm进行安装。

Any ideas where I'm going wrong? 有什么想法我要去哪里吗?

Thanks! 谢谢!

That's an async call you have to await it. 这是一个异步调用,您必须等待它。 await web3.utils.fromWei(this.state.balance, 'ether') should do. await web3.utils.fromWei(this.state.balance, 'ether')应该执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM