简体   繁体   English

React js:./src/App.js第22行:未定义'lastWinner'no-undef搜索关键字以了解有关每个错误的更多信息

[英]React js: ./src/App.js Line 22: 'lastWinner' is not defined no-undef Search for the keywords to learn more about each error

Hello all I'm learning the basics of react, and how to program in solidity. 您好,我正在学习react的基础知识,以及如何扎实地编程。 I recently completed this smart contract that functions as a lottery and have full functionality. 我最近完成了这份智能合约,该彩票具有彩票功能并具有全部功能。 I have gone back and now want to display the address of the winner who will receive the funds from the lottery but have encountered this error 我回去了,现在想显示中奖者的地址,该中奖者将从彩票中获得资金,但遇到此错误

./src/App.js Line 22: 'lastWinner' is not defined no-undef ./src/App.js第22行:“ lastWinner”未定义no-undef

Search for the keywords to learn more about each error. 搜索关键字以了解有关每个错误的更多信息。

Here is the main react component *note- have have starred/ bolded the area where the error is 这是主要的反应成分*注意-已加星号/加粗了错误所在的区域

import React, { Component } from 'react';
//import logo from './logo.svg';
import './App.css';
import web3 from './web3';
import lottery from './lottery';

class App extends Component {
    state ={
      manager: ' ',
      players: [] , // players set to empty array
      balance: ' ',
      value: ' ',
      message: ' ',
      **lastWinner:' ',**
  }; //inatilizing state

  async componentDidMount() {
    const manager = await lottery.methods.manager().call();// retrieve manager from ETH ntwrk
    const winner = await lottery.methods.pickWinner().call(); // displays last winner address
    const players = await lottery.methods.getPlayer().call(); // get number of players
    const balance = await web3.eth.getBalance(lottery.options.address); // get contract's balance
    this.setState({manager, players, balance, **lastWinner**});// set state object
    }

    onSubmit= async (event) => { // function that calls contract entry
      event.preventDefault(); //makes sure form doesnt auto submit itself
      const accounts = await web3.eth.getAccounts(); // get accounts enterd in lotto
      this.setState({message: 'Waiting on transaction to be processed'}); //tells users whats going on with application
      //asume 1st account in array is entering lotto
      await lottery.methods.enter().send({
        from: accounts [0],
        value: web3.utils.toWei(this.state.value, 'ether')

      });
      this.setState({message: 'You have been entered'}); // update message
    };

    onClick = async () => {
      const accounts = await web3.eth.getAccounts(); // get accounts enterd in lotto
      this.setState({message: 'Waiting on transaction to be processed'});
      await lottery.methods.pickWinner().send({ // calls picks winner function
        from: accounts[0]
      });
      this.setState({message: 'Winner has been picked'});
    };
  render() {



    return (
      <div>
        <h2>Lottery Contract</h2>
        <p>
        This contract is managed by {this.state.manager}.
        Currently {this.state.players.length} people entered in the lottery.
        The lottery is valued at {web3.utils.fromWei(this.state.balance, 'ether')} ether!
          The last person to win was: {this.state.lastWinner}.
        </p>

        <hr/>

        <form onSubmit ={this.onSubmit}>
          <h4>Want to try your luck?</h4>
            <div>
              <label>Amount of ETH to enter. </label>
                <input
                value = {this.state.value}
                onChange={event => this.setState({value: event.target.value })} // updates a prop. called value which hold amount of ether

                />
            </div>
              <button>Enter</button>
        </form>

        <hr/>
          <h4>Ready to Pick a winner?</h4>
          <button onClick={this.onClick}> Pick~Winner</button>
        <hr/>
        <h1>{this.state.message}</h1>
      </div>
    );
  }
}

export default App;

also here is my code for the Ethereum Contract 这也是我的以太坊合约代码

pragma solidity  ^0.4.17;

contract Lottery{
  address public manager;
  address[] public players;
  address public lastWinner;

function Lottery() public{ // starting of lottery creation contract. assigns the person who calls this contract as manager.
  manager = msg.sender;
}

function enter() public payable { // entry function to contract- requires that entrants must submit at lesat .01ETH
  require(msg.value  > 0.01 ether);

  players.push(msg.sender);
}

function random() private view returns (uint){ // function that CALCULATES a winner based on current block diff. present time, and # of players
  return uint(keccak256(block.difficulty, now, players));//note* ispsudo random, can be reverse engineered.
}

function pickWinner() public restricted{ //inclusion of function modifier named 'restricted'
  //require (msg.sender == manager);  --Including the function modifier 'restriced' reduces the need for this duplicate line

  uint index = random() % players.length; //uses random calculation and modulods op to calculate a winner from index of players
  players[index].transfer(this.balance); //sends lottery funds to the index positioned winner
  players = new address[](0); // resets players in contract index back to 0
  lastWinner = players[index]; // records address of person who last won lottery
  lastWinner = msg.sender;
  //return lastWinner;
}

modifier restricted(){ // function modifier- purpose to reduce the need for repetitive code. can be applied on any function
  require (msg.sender == manager);
  _;
}

function getPlayer()public view returns(address[]){ //function that returns the full list of players entered into the lottery
  return players;  // public- seen by everyone~ view- does not modify contract data~ should return a dynamic array of addys.
}

}

In the line: 在该行中:

this.setState({manager, players, balance, **lastWinner**});// set state object

You are trying to pass in a variable called lastWinner but the variable you defined for the winner is called winner : 您正在尝试在一个名为变量传递lastWinner ,但你为胜利者定义的变量称为winner

const winner = await lottery.methods.pickWinner().call(); // displays last winner address

Either change the variable name to lastWinner: 可以将变量名称更改为lastWinner:

const lastWinner = await lottery.methods.pickWinner().call(); // displays last winner address

Or pass in the winner variable with the lastWinner key: 或使用lastWinner键传递winner变量:

this.setState({manager, players, balance, lastWinner: winner});

暂无
暂无

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

相关问题 src\components\cards.js Line 75:68: 'index' is not defined no-undef 搜索关键字以了解有关每个错误的更多信息 - src\components\cards.js Line 75:68: 'index' is not defined no-undef Search for the keywords to learn more about each error ./src/App.js Line 6:19: &#39;Component&#39; 未定义 no-undef - ./src/App.js Line 6:19: 'Component' is not defined no-undef &#39;Component&#39; 未定义 no-undef 搜索关键字以了解有关每个错误的更多信息 - 'Component' is not defined no-undef Search for the keywords to learn more about each error React Js:未定义mountNode no-undef错误 - React Js: mountNode is not defined no-undef error ./src/index.js 第 10 行:'elementid' 未定义 no-undef - ./src/index.js Line 10: 'elementid' is not defined no-undef 如何在React“ ./src/components/App.js&#39;Layout&#39;,&#39;Home&#39;中定义组件错误,未定义react / jsx-no-undef - How to fix component error in React "./src/components/App.js 'Layout', 'Home' is not defined react/jsx-no-undef 如何修复 React.js 中的““X”未定义 no-undef”错误 - How to fix ' "X" is not defined no-undef' error in React.js 当前 React JS:&#39;state&#39; 未定义 no-undef - Current React JS : 'state' is not defined no-undef [eslint] src\App.js 第 2:8 行中的 React js 错误警告:'person' 已定义但从未使用过 - React js error WARNING in [eslint] src\App.js Line 2:8: 'person' is defined but never used 在./src/App.js&#39;store&#39;中的反应错误未定义 - react Error in ./src/App.js 'store' is not defined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM