0

I'm trying to build a Student Management System Dapp,where data of students are taken from frontend and stored in smart contract. Why I'm getting this error,while I try to set value(name) to the field-name.What changes do I need to make? The below is my code please check what's wrong with it. The below is my App.js code

App.js

import React, { useState } from 'react';
import Web3 from 'web3';
import { simpleStorageAbi } from './abi/abis';
import './App.css';
const web3 = new Web3(Web3.givenProvider);
// contract address is provided by Truffle migration
const contractAddr = '0xd9145CCE52D386f254917e481eB44e9943F39138';
const SimpleContract = new web3.eth.Contract(simpleStorageAbi, contractAddr);
function App() {
  const [name, setName] = useState();
  const [getName, setGetName] = useState();

  //const [dept, setDept] = useState();
  //const [getDept, setGetDept] = useState();

  const handleSet = async (e) => {
    e.preventDefault();    
    const accounts = await window.ethereum.enable();
    const account = accounts[0];
    const gas = await SimpleContract.methods.set(name).estimateGas();
    const result = await SimpleContract.methods.set(name).send({
      from: account,
      gas 
    })
    console.log(result);
  }

  const handleGet = async (e) => {
    e.preventDefault();
    const result = await SimpleContract.methods.get().call();
    setGetName(result);
    console.log(result);
  }

return (
  <div className="App">
    <header className="App-header">
      <form onSubmit={handleSet}>
        <label>
          Name:
          <input 
            type="text"
            name="name"
            value={name}
            onChange={ e => setName(e.target.value) } />
        </label>
        <input type="submit" value="Set Name" />
      </form>
      <br/>
      <button
        onClick={handleGet}
        type="button" > 
        Get Name 
      </button>
      { getName }
    </header>
  </div>  
);
};
export default App;

The below is my Solidity code. SimpleStorage.sol

pragma solidity >=0.4.22 <0.9.0;
contract SimpleStorage{
    string name;
    string dept;
    uint phonenumber;
    string addresshome;
    string emailid;
    uint aadhar;
    string ipfshash1;
    string ipfshash2;
    string ipfshash3;
    string ipfshash4;
    constructor() public {
        phonenumber=0;
        aadhar=0;
        
    }
    
    function setname(string memory _name) public  returns(string memory){
        name=_name;
    }
    
    function getname() public view returns(string memory){
        return name;
    }
    function getdept(string memory _dept) public pure returns(string memory){
        return _dept;
    }
    
    function getphonenumber(uint _phonenumber) public pure returns(uint){
        return _phonenumber;
    }
    
    function getaddresshome(string memory _addresshome) public pure returns(string memory){
        return _addresshome;
    }
    
    function getemailid(string memory _emailid) public pure returns(string memory){
        return _emailid;
    }
    
    function getaadhar(uint _aadhar) public pure returns(uint){
        return _aadhar;
    }
}
0

Your contract has no function named set().

Your contract has a function named setname() instead.

(The same holds true for get() -> getname().)

2
  • oops ya I found that. My doubt now is where can I get my contract address in solidity?
    – Shru
    Jun 4 at 13:39
  • index.js:298 Uncaught (in promise) Error: Returned values aren't valid, did it run Out of Gas? You might also see this error if you are not using the correct ABI for the contract you are retrieving data from, requesting data from a block number that does not exist, or querying a node which is not fully synced. Am getting this when I try to get my name back
    – Shru
    Jun 4 at 13:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.