Here is my contract -
pragma solidity ^0.5.0;
pragma experimental ABIEncoderV2;
contract SimpleStorage {
enum Status { NotExist, Created, InTransit, PaidFor, Completed }
struct Property {
Status state;
uint price;
address currOwner;
}
Property[] public arr;
mapping (address => uint) balances;
Property public house;
function registerProperty(uint _price) public {
house = Property(Status.Created, _price,msg.sender);
//emit NewProperty(msg.sender, _price);
}
function get()public returns(Property[] memory){
return arr;
}
}
Here is my app.js. If I would return only a single value it will not show any error, but I want an array to be returned.
import React, { Component } from "react";
import SimpleStorageContract from "./contracts/SimpleStorage.json";
import getWeb3 from "./utils/getWeb3";
import "./App.css";
class App extends Component {
state = { storageValue:"", web3: null, accounts: null, contract: null,newValue:"" };
componentDidMount = async () => {
try {
this.handleChange=this.handleChange.bind(this);
this.handleSubmit=this.handleSubmit.bind(this);
// Get network provider and web3 instance.
const web3 = await getWeb3();
// Use web3 to get the user's accounts.
const accounts = await web3.eth.getAccounts();
// Get the contract instance.
const networkId = await web3.eth.net.getId();
const deployedNetwork = SimpleStorageContract.networks[networkId];
const instance = new web3.eth.Contract(
SimpleStorageContract.abi,
deployedNetwork && deployedNetwork.address,
);
// Set web3, accounts, and contract to the state, and then proceed with an
// example of interacting with the contract's methods.
this.setState({ web3, accounts, contract: instance }, this.runExample);
} catch (error) {
// Catch any errors for any of the above operations.
alert(
`Failed to load web3, accounts, or contract. Check console for details.`,
);
console.error(error);
}
};
handleChange(event){
this.setState({newValue: event.target.value });
}
handleSubmit = async event => {
const { contract, accounts } = this.state;
event.preventDefault();
await contract.methods.registerProperty(this.state.newValue).send({ from: accounts[0] });
const {response} = await contract.methods.get().call();
this.setState({ storageValue: response });
}
runExample = async () => {
const {contract } = this.state;
// Stores a given value, 5 by default.
// await contract.methods.set("").send({ from: accounts[0] });
// Get the value from the contract to prove it worked.
const response = await contract.methods.get().call();
// Update state with the result.
this.setState({ storageValue: response});
};
render() {
//if (!this.state.web3) {
// return <div>Loading Web3, accounts, and contract...</div>;
//}
return (
<div className="App">
<h1>Good to Go!</h1>
<div>The stored value is: {this.state.storageValue}</div>
<form onSubmit={this.handleSubmit}>
<input type="text" value ={this.state.newValue} onChange={this.handleChange.bind(this)} />
<input type="submit" value="submit" />
</form>
</div>
);
}
}
export default App;