首页 > 解决方案 > methods.myFunction().call() throws 返回值是无效的,它是否耗尽了 Gas?错误

问题描述

我正在使用“web3”:“1.0.0-beta.26”并收到以下错误...

返回值无效,它是否用尽了 Gas?如果您没有为从中检索数据的合约使用正确的 ABI、从不存在的块号请求数据或查询未完全同步的节点,您也可能会看到此错误。

我已经重新运行 truffle migrate --reset 并使用生成的地址作为 TODO_LIST_ADDRESS 但我仍然收到上面的错误

有人遇到过这个问题并且知道如何解决吗?谢谢

应用程序.js

class App extends Component {
  constructor(props) {
    super(props);
    this.state = {
      account: "test",
      taskCount: 0
    };
  }

  componentDidMount() {
    this.loadBlockchainData();
  }

  loadBlockchainData = async () => {
    const ethereum = window.ethereum;
    ethereum.autoRefreshOnNetworkChange = false;
    const web3 = window.web3;
    const web3Instance = new Web3(ethereum);
    const enabledWeb3 = await ethereum.enable();
    const accounts = await web3Instance.eth.getAccounts();
    const accountAddress = await accounts[0];
    this.setState({ account: accountAddress });
    const todoList = new web3Instance.eth.Contract(
      TODO_LIST_ABI,
      TODO_LIST_ADDRESS
    );
    console.log("todoList ", todoList);

    //! ERROR 
    const taskCount = await todoList.methods.taskCount().call();
    console.log("taskCount ", taskCount);
    this.setState({ taskCount });
  };

  render() {
    return (
      <div className="container">
        <h1>Hey World</h1>
        <p>Your Account: {this.state.account}</p>
        <p>Task Count: {this.state.taskCount}</p>
      </div>
    );
  }
}

export default App;

配置.js

export const TODO_LIST_ADDRESS = "0xb0D64f6C317448efa56A33678d718Fd715DAeddd";
export const TODO_LIST_ABI = [
  {
    inputs: [],
    payable: false,
    stateMutability: "nonpayable",
    type: "constructor"
  },
  {
    constant: true,
    inputs: [],
    name: "taskCount",
    outputs: [
      {
        internalType: "uint256",
        name: "",
        type: "uint256"
      }
    ],
    payable: false,
    stateMutability: "view",
    type: "function"
  },
  {
    constant: true,
    inputs: [
      {
        internalType: "uint256",
        name: "",
        type: "uint256"
      }
    ],
    name: "tasks",
    outputs: [
      {
        internalType: "uint256",
        name: "id",
        type: "uint256"
      },
      {
        internalType: "string",
        name: "content",
        type: "string"
      },
      {
        internalType: "bool",
        name: "completed",
        type: "bool"
      }
    ],
    payable: false,
    stateMutability: "view",
    type: "function"
  },
  {
    constant: false,
    inputs: [
      {
        internalType: "string",
        name: "_content",
        type: "string"
      }
    ],
    name: "createTask",
    outputs: [],
    payable: false,
    stateMutability: "nonpayable",
    type: "function"
  }
];

标签: reactjsblockchainsolidity

解决方案


推荐阅读