Die konstante Funktion gibt ein leeres Array + web3js zurück

Ich habe ein einfaches Solidity-Skript, in dem ich in Remix getestet habe (injiziertes Web3 mit der lokalen Geth-Umgebung) und gut funktionierte, als ich ein Array von Daten aus einer konstanten Methode zurückgab.

pragma solidity ^0.4.11;

contract TestBetting {

  struct BettingInfo {    
      uint256 matchId;
      uint homeTeamScore;
      uint awayTeamScore;     
      uint bettingPrice;  
  }

  address public owner;
  mapping(address => BettingInfo[]) public bettingInfo;

 // constructor
  function MyBetting() {
    owner = msg.sender;
  }

  // Fall back 
  function () payable {}

  event LogDeposit(address sender, uint amount);

  // Place a bet  
  function placeBet(uint256 _matchId, 
                    uint _homeTeamScore, 
                    uint _awayTeamScore, 
                    uint _bettingPrice) payable returns (bool) {

    bettingInfo[msg.sender].push(
      BettingInfo(_matchId, _homeTeamScore, _awayTeamScore, _bettingPrice)); 

    require(_bettingPrice == msg.value); 
    this.transfer(msg.value); 

    LogDeposit(msg.sender, msg.value);

    return true;
  }

  function getBettingInfo(address _better) public constant returns (uint256[], uint[], uint[], uint[]) {
    uint length = bettingInfo[_better].length;
    uint256[] memory matchId = new uint256[](length);
    uint[] memory homeTeamScore = new uint[](length);
    uint[] memory awayTeamScore = new uint[](length);
    uint[] memory bettingPrice = new uint[](length);   

    for (uint i = 0; i < length; i++) {
      matchId[i] = bettingInfo[_better][i].matchId;
      homeTeamScore[i] = bettingInfo[_better][i].homeTeamScore;
      awayTeamScore[i] = bettingInfo[_better][i].awayTeamScore;
      bettingPrice[i] = bettingInfo[_better][i].bettingPrice;   
    }

    return (matchId, homeTeamScore, awayTeamScore, bettingPrice);
  }
}

Geben Sie hier die Bildbeschreibung ein

Wenn ich dies in Winkel 5 mit der lokalen Geth-Umgebung mache, muss ich zuerst die Transaktion über die placeBet-Methode einreichen.

submit(form: NgForm): void {  
    this.bettingPrice = 20;
    this.bettingPrice = this.web3.toWei(this.bettingPrice, "ether");

    this.TestBetting.deployed().then((instance) => {
        return instance.placeBet(this.matchId, 
                                 this.homeTeamScore, 
                                 this.awayTeamScore, 
                                 this.bettingPrice, {
            from: this.account,
            value: this.bettingPrice,
            gas: 50000
        });
      })
      .then((value) => {
        console.log(value);
      })
      .catch((e) => {
        console.log(e);
      }); 
  }

Es öffnet sich die Metamaske und es sieht so aus, als wäre sie durchgegangen.Geben Sie hier die Bildbeschreibung ein

Jetzt möchte ich getBettingInfo anrufen, um die Daten zu sehen, die ich an das Netzwerk übermittelt habe.

  loadGetBettingInfo() {   
    this.TestBetting.deployed().then((instance) => {
        return instance.getBettingInfo.call(this.betterAddress, {
            from: this.betterAddress,
            gas: 50000
        });
      })
      .then((value) => {
        console.log(value);
      })
      .catch((e) => {
        console.log(e);
      }); 
    }

Dies gibt jedoch ein leeres Array in der Konsole zurück.Geben Sie hier die Bildbeschreibung ein

Kann jemand sehen, was ich hier falsch mache?

Sind Sie sicher, dass die Transaktion korrekt abgebaut wurde? Vielleicht rufen Sie getBettingInfo an, bevor die Transaktion abgebaut wird. Ist betterAddress dieselbe Adresse, die die Wette platziert hat?
Ja, ich habe mit Geth geschürft und bin zu Ganache gewechselt und es funktioniert jetzt. geth --networkid 4224 --mine --datadir "..path" --nodiscover --rpc --rpcport "8545" --port "...port #" --rpccorsdomain "*" --nat "any" --rpcapi eth,web3,personal,miner,net --unlock 0 --password password.sec Dies war mein Geth-Befehl, in dem meiner aktiviert war. Weiß immer noch nicht, warum es damit nicht geklappt hat

Antworten (1)

Dies liegt daran, this.TestBetting.deployed().then((instance)dass jedes Mal eine neue Instanz erstellt wird, die nicht über die vorherigen Statusänderungen verfügt. Sie müssen also eine Instanz erstellen und diese für den Zugriff auf alle Funktionen verwenden.

Contract.deployed()erstellt keine neue Instanz, sondern gibt nur einen Verweis auf die Instanz zurück, die mit den Migrationsskripten erstellt wurde.