gpt4 book ai didi

reactjs - 出现错误 : TypeError: Cannot read property 'getId' of undefined in React + truffle DApp

转载 作者:行者123 更新时间:2023-12-03 14:24:56 24 4
gpt4 key购买 nike

我的 Ganche-GUI 和元掩码正在运行。但我收到以下错误在控制台中

类型错误:无法读取未定义的属性“getId” 在 App.componentDidMount (App.js:17)

以下是我在App.js代码中的代码

import React, {Component} from 'react';
import logo from './logo.svg';
import './App.css';
import getWeb3 from './utils/getweb3';
import CrowdFundingContract from './contracts/CrowdFunding.json';

class App extends Component {
state = {web3: null, accounts: null, contracts: null};

componentDidMount = async () => {
try{
const web3 = await getWeb3();

const accounts = await web3.eth.getAccounts();

const networkId = await web3.eth.net.getId();

const deployedNetwork = CrowdFundingContract.networks[networkId];

const instance = new web3.eth.Contract(CrowdFundingContract.abi, deployedNetwork && deployedNetwork.address);

this.setState({web3, accounts, contract: instance}, this.runExample);
}
catch(error){
alert('Failed to load web3, accounts and contract, check the console');
console.error(error);
}
};

}

我没有把 render() 和函数 returnExample 放在这里。

请帮助我

最佳答案

您是否连接到 eth 提供商?

    const Web3 = require('web3')
const web3 = new Web3(new Web3.providers.HttpProvider('https://mainnet.infura.io'))

您确定使用的是 web3 1.x.x 吗?因为您的代码看起来不错,所以如果您使用 web3 v0.x.x,请尝试以下代码:

    web3.version.getNetwork(function(err, res){
let selectedNetwork = ""

if (!err) {
switch (res) {
case "1":
selectedNetwork = "Mainnet"
break
case "2":
selectedNetwork = "Morden"
break
case "3":
selectedNetwork = "Ropsten"
break
case "4":
selectedNetwork = "Rinkeby"
break
default:
selectedNetwork = "Unknown network = "+res
}
return selectedNetwork
} else {
console.log(err)
}
})

关于reactjs - 出现错误 : TypeError: Cannot read property 'getId' of undefined in React + truffle DApp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56168872/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com