未处理的拒绝类型错误:无法读取未定义的属性“投票”

问题描述

我正在 Dapps 中练习。每次我尝试发送交易时,我都会收到此错误未处理的拒绝(类型错误):无法读取未定义的属性“投票”。但是有了通话功能,一切都很好。我看了很多视频,没有人遇到同样的问题。我已经检查了代码,对我来说很好,我已经在代码中确定了函数“投票”,其余的都很好。我找不到解决方案,所以我完全绝望了(

我正在使用 React js。

import React,{useEffect,useState} from 'react';
import Web3 from 'web3';
import Pleaseabi from './contracts/please.json';
import Navbar from './Navbar'
import Body from './Body'

function App() {

  useEffect(() => {
    loadWeb3();
    LoadBlockChaindata();
  },[])

const[CurrentAccount,setCurrentaccount] = useState("")
const[loader,setloader] = useState(true);
const[Election,SetElection] = useState();
const[Candidate1,setCandidate1] = useState();
const[Candidate2,setCandidate2] = useState();


  const loadWeb3 = async() => {
    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-ether"
        );
    }
};

const LoadBlockChaindata = async() =>{
  setloader(true);
  const web3 = window.web3;
  
  const accounts = await web3.eth.getAccounts();
  const account = accounts[0];
  setCurrentaccount(account);
  const networkId = await web3.eth.net.getId();
  
  const networkData = Pleaseabi.networks[networkId];

  if(networkData){
    const election = new web3.eth.Contract(Pleaseabi.abi,networkData.address );
    const candidate1 = await election.methods.candidates(1).call();
    const candidate1id = candidate1.id;
    const candidate2 = await election.methods.candidates(2).call();
    setCandidate1(candidate1);
    setCandidate2(candidate2);
    setloader(false);
    SetElection(election);
  }else(window.alert('The smart contract is deployed to another network'));
};

const Votecandidate = async(candidateid) =>{
  setloader(true);
  await Pleaseabi.methods.Vote(candidateid).send({from:CurrentAccount}).on('transactionhash',()=>{ \\  error happens here
    console.log('success')
  });
  setloader(false);
};

if(loader){
  return <div>loading...</div>
}

  return (
    <div>
      <Navbar account={CurrentAccount}/>
      <Body candidate1={Candidate1} candidate2={Candidate2} Votecandidate={Votecandidate} account={CurrentAccount}/>
    </div>
  );
}

export default App;

solidity代码

pragma solidity ^0.5.16;

contract please{
    struct Candidate{
        uint id;
        string name;
        uint Votecount;
    }
    uint public candidatesCount;
    
    mapping(uint => Candidate) public candidates;
    
    mapping (address=>bool) public Votedornot;
    
    
    event electionUpdated(uint id,string name,uint Votecount);
    
    constructor() public{
        addCandidate('Donald');
        addCandidate('Obama');
    }
    
    function addCandidate(string memory name) private{
        candidatesCount++;
        candidates[candidatesCount] = Candidate(candidatesCount,name,0 );
        
    }
    function Vote(uint _id) public{
        require (!Votedornot[msg.sender],"u've Voted");
        require(candidates[_id].id != 0,'not exist');
        candidates[_id].Votecount+=1;
        Votedornot[msg.sender] = true;
        emit electionUpdated(_id,candidates[_id].name,candidates[_id].Votecount);
    }
    
}

感谢您的帮助。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)