简体   繁体   English

尝试从 react js 中获取 Solidity 智能合约中的数据时返回 NULL 值

[英]NULL value returned when trying to fetch data from solidity smart contract from react js

I'm trying to make a Student Management System using Ethereum blockchain.我正在尝试使用以太坊区块链制作学生管理系统。 I've linked my smart contract (solidity) with react frontend.我已将我的智能合约(solidity)与 react 前端联系起来。 I try to get the setted value from the smart contract, but this returns "null" in the console.我尝试从智能合约中获取设置的值,但这会在控制台中返回“null”。 But the smart contract works fine.但是智能合约运行良好。 I'll link my smart contract and the App.js code below.我将链接我的智能合约和下面的 App.js 代码。 Please help me to solve the issue.请帮我解决这个问题。 As I'm new to the development I'm not much aware of it.由于我是开发的新手,所以我不太了解它。 Thanks in advance.提前致谢。

Smart contract code智能合约代码

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 {
        name=_name;
    }
    
    function getname() public view returns(string memory){
        return name;
    }
    function setdept(string memory _dept) public {
        dept=_dept;
    }
    function getdept() public view returns(string memory){
        return dept;
    }
    function setphonenumber(uint _phonenumber) public{
        phonenumber=_phonenumber;
    }

    function getphonenumber() public view returns(uint){
        return phonenumber;
    }
    
    function setaddresshome(string memory _addresshome) public {
        addresshome=_addresshome;
    }
    function getaddresshome() public view returns(string memory ){
        return addresshome;
    }
    function setemailid(string memory _emailid) public {
        emailid=_emailid;
    }
    function getemailid() public view returns(string memory){
        return emailid;
    }
    
    function setaadhar(uint _aadhar) public {
        aadhar=_aadhar;
    }
    function getaadhar() public view returns(uint){
        return aadhar;
    }
}

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 = '0x262E2078217937978cccd3a08681AdD77D5DdFE3';
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 [phonenumber, setPhonenumber] = useState();
  const [getPhonenumber, setGetPhonenumber] = useState();

  const [homeaddress, setHomeaddress] = useState();
  const [getHomeaddress, setGetHomeaddress] = useState();

  const [emailid, setEmailid] = useState();
  const [getEmailid, setGetEmailid] = useState();

  const [aadhar, setAadhar] = useState();
  const [getAadhar, setGetAadhar] = useState();



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

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

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

  const handleDeptGet = async (e) => {
    e.preventDefault();
    const result = await SimpleContract.methods.getdept().call();
    setGetDept(result);
    console.log(result);
  }


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

  const handlePhonenumberGet = async (e) => {
    e.preventDefault();
    const result = await SimpleContract.methods.getphonenumber().call();
    setGetPhonenumber(result);
    console.log(result);
  }
  

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

  const handleHomeaddressGet = async (e) => {
    e.preventDefault();
    const result = await SimpleContract.methods.getaddresshome().call();
    setGetHomeaddress(result);
    console.log(result);
  }

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

  const handleEmailidGet = async (e) => {
    e.preventDefault();
    const result = await SimpleContract.methods.getemailid().call();
    setGetEmailid(result);
    console.log(result);
  }

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

  const handleAadharGet = async (e) => {
    e.preventDefault();
    const result = await SimpleContract.methods.getaadhar().call();
    setGetAadhar(result);
    console.log(result);
  }

return (
  <div className="App">
    <header className="App-header">
      <h1> Student Details </h1>
    </header>
    <div className="wrapper">
      <form className= "Form" onSubmit={handleNameSet}>
        <label>
          <h3 style={{textAlign:"left",marginBottom:"0.5px",fontFamily:"unset"}}>NAME:</h3>
          <input className="Form"
            type="text"
            name="name"
            value={name}
            onChange={ e => setName(e.target.value) } />
        </label>
        <input className="body" type="submit" value="Set it" />
        <button
        className="body"
        onClick={handleNameGet}
        type="button" > 
        Get it
      </button>
      { getName }
      </form>


      <form className= "Form" onSubmit={handleDeptSet}>
      <label>
          <h3 style={{textAlign:"left",marginBottom:"0.5px",fontFamily:"unset"}}>DEPARTMENT:</h3>
          <input className="Form"
            type="text"
            name="dept"
            value={dept}
            onChange={ e => setDept(e.target.value) } />
        </label>
        <input className="body" type="submit" value="Set it" />
        <button
        className="body"
        onClick={handleDeptGet}
        type="button" > 
        Get it 
      </button>
      { getDept }
       </form>

       <form className= "Form" onSubmit={handlePhonenumberSet}>
      <label>
          <h3 style={{textAlign:"left",marginBottom:"0.5px",fontFamily:"unset"}}>PHONE NUMBER:</h3>
          <input className="Form"
            type="text"
            name="dept"
            value={phonenumber}
            onChange={ e => setPhonenumber(e.target.value) } />
        </label>
        <input className="body" type="submit" value="Set it" />
        <button
        className="body"
        onClick={handlePhonenumberGet}
        type="button" > 
        Get it 
      </button>
      { getPhonenumber }
       </form>


       <form className= "Form" onSubmit={handleHomeaddressSet}>
        <label>
          <h3 style={{textAlign:"left",marginBottom:"0.5px",fontFamily:"unset"}}>ADDRESS:</h3>
          <input className="Form"
            type="text"
            name="name"
            value={homeaddress}
            onChange={ e => setHomeaddress(e.target.value) } />
        </label>
        <input className="body" type="submit" value="Set it" />
        <button
        className="body"
        onClick={handleHomeaddressGet}
        type="button" > 
        Get it
      </button>
      { getHomeaddress }
      </form>

      <form className= "Form" onSubmit={handleEmailidSet}>
        <label>
          <h3 style={{textAlign:"left",marginBottom:"0.5px",fontFamily:"unset"}}>EMAIL ID:</h3>
          <input className="Form"
            type="text"
            name="name"
            value={emailid}
            onChange={ e => setEmailid(e.target.value) } />
        </label>
        <input className="body" type="submit" value="Set it" />
        <button
        className="body"
        onClick={handleEmailidGet}
        type="button" > 
        Get it
      </button>
      { getEmailid }
      </form>

      <form className= "Form" onSubmit={handleAadharSet}>
        <label>
          <h3 style={{textAlign:"left",marginBottom:"0.5px",fontFamily:"unset"}}>AADHAR NO.:</h3>
          <input className="Form"
            type="text"
            name="name"
            value={aadhar}
            onChange={ e => setAadhar(e.target.value) } />
        </label>
        <input className="body" type="submit" value="Set it" />
        <button
        className="body"
        onClick={handleAadharGet}
        type="button" > 
        Get it
      </button>
      { getAadhar }
      </form>

      <br/>
      </div>
      
      <button className="files" type="button" >Add files..</button>

  </div>  
);
};
export default App;

You can check in your "abi".你可以签入你的“abi”。 I have resolved the same issue by changing the "outputs" key in abi as below.我通过更改 abi 中的“输出”键解决了同样的问题,如下所示。

const abi = [
    {
      constant: true,
      inputs: [],
      name: 'manager',
      outputs: [{ name: '', type: 'address' }],
      payable: false,
      stateMutability: 'view',
      type: 'function',
    }]

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

相关问题 Solidity:从我的智能合约中显示价值以做出反应的问题 - Solidity : Problem to display value from my smart contract to react 通过javascript(web3.js)从部署的智能合约中调用特定的solidity函数 - Call specific solidity function from deployed smart contract within react through javascript (web3.js) 如何从 Javascript 向 Solidity 智能合约 function 输入数据? - How to input data to a solidity smart contract function from Javascript? 使用Web3.js将价值从Solidity合同转移到Ganache帐户 - Transfer Value From Solidity Contract To Ganache Account With Web3.js 使用智能合约中的项目更新 React JS 中的 state 数组 - Update state array in React JS with item from Smart Contract 从React中的一个单独的.js文件导出由&#39;fetch&#39;返回的数据 - Export data returned by 'fetch' from a separate .js file in React 来自solidity合约的app.js文件中的getJSON()出错 - Getting error in getJSON() in app.js file from solidity contract 智能合约:如何在 React 中获得 Solidity function 的回报? - Smart contract: How to get return of Solidity function in React? 是否可以从特定帐户在我的前端运行我的 Solidity 智能合约 function - Is it possible to run my Solidity Smart Contract function on my frontend from a specific account Solidity:在智能合约中填充结构 - Solidity: populating a struct in a smart contract
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM