web3js 给出 null 作为有效智能合约函数的输出

问题描述 投票:0回答:1

我正在观看 dapp 大学关于构建 dapp 的 yt 教程,并按照他的视频进行操作,直到遇到这个问题。他试图建立一个社交媒体网站,用户可以在其中发帖,打赏者可以打赏加密货币。

这是可靠性代码:

pragma solidity ^0.5.0;

contract SocialNetwork{
    string public name;
    uint public postCount = 0;
    mapping(uint => Post) public posts;

    struct Post{
        uint id;
        string content;
        uint tipAmount;
        address payable author;
    }

    event PostCreated(
        uint id,
        string content,
        uint tipAmount,
        address payable author
    );
    event PostTipped(
        uint id,
        string content,
        uint tipAmount,
        address payable author
    );

    constructor() public {
        name = "My network";
    } 

    function createPost(string memory _content) public{

        require(bytes(_content).length > 0);

        postCount++;
        posts[postCount] = Post(postCount,_content,0,msg.sender);
        emit PostCreated(postCount, _content,0, msg.sender);
    }

    function tipPost(uint _id) public payable{
        
        require(_id>0 && _id<=postCount);

        Post memory _post = posts[_id];
        address payable _author = _post.author;
        address(_author).transfer(msg.value);
        _post.tipAmount = _post.tipAmount+ msg.value;
        posts[_id] = _post;

        emit PostTipped(postCount, _post.content, _post.tipAmount, _author);
    }
}

这是客户端反应代码:

class App extends Component {

  async componentWillMount(){
    await this.loadWeb3();
    await this.loadBLockCHainData()
  }

  async loadWeb3(){
    if (window.ethereum) {
        window.web3 = new Web3(window.ethereum);
        await window.ethereum.enable();
    }
    else if (window.web3) {
        window.web3 = new Web3(window.web3.currentProvider);
    }
    else {
      window.alert("Non eth browser");
        console.log('Non-Ethereum browser detected. You should consider trying MetaMask!');
    }
  }

  async loadBLockCHainData(){
    const web3 = window.web3
    const accounts = await web3.eth.getAccounts();
    console.log(accounts)
    this.setState({account:accounts[0]});

    const networkId = await web3.eth.net.getId();
    console.log(networkId)
    const networkData = SocialNetwork.networks[networkId]
    if(networkData){
      const socialNetwork = web3.eth.Contract(SocialNetwork.abi, networkData.address)
      this.setState({socialNetwork})
      const postCount = await socialNetwork.methods.postCount().call();
      console.log(postCount)
      this.setState({ postCount: postCount});
    }
    else{
      window.alert('Social Network not deployed to block chain')
    }
  }

  constructor(props){
    super(props);
    this.state={
      account :'',
      socialNetwork:null,
      postCount:0,
    }
  }
}

问题是 console.log(postCount) 为我打印 null,而为他打印 BN(2),因为他创建了 2 个帖子。即使我已经从 truffle 终端创建了 2 个帖子。

我尝试从 truffle 终端调用 postCount 并按预期工作,但在客户端 web3js Reactjs 代码中无法按预期工作。

reactjs web3js truffle
1个回答
0
投票

我在您的代码中观察到的一件事是您可能没有正确初始化合约。

const socialNetwork = web3.eth.Contract(SocialNetwork.abi, networkData.address)

您应该在

new
关键字的帮助下初始化合约。例如:

const socialNetwork = new web3.eth.Contract(SocialNetwork.abi, networkData.address)

更多细节可以参考web3.js官方文档来初始化新合约。 https://web3js.readthedocs.io/en/v1.2.11/web3-eth-contract.html#new-contract

还有一点是,请确保您的React应用程序中的合约地址

networkData.address
与Sepolia测试网上部署的合约地址相匹配。

© www.soinside.com 2019 - 2024. All rights reserved.