reactjs - 未处理的拒绝(TypeError):BlocContract.methods.read_task 不是函数
问题描述
我正在学习如何成为一名区块链开发人员,目前正在尝试使用 truffle 部署一个以太坊待办事项列表。但是,我在标题中遇到错误,并且不知道我做错了什么。作为参考,我使用的是 web3 版本 1.4.0,并在下面附上了我的代码。
Solidity 合约代码
//Bloc.sol
pragma solidity >=0.4.22 <0.9.0;
contract Bloc {
struct Task {
bool status;
string task;
}
//msg.sender gives us the address of the user
mapping (address => Task[]) public todo_list;
Task[] public todo_arr;
function read_task(uint user_index) public view returns (string memory) {
return todo_list[msg.sender][user_index].task;
}
function add_task(string memory task) public { //external functions are cheaper than public functions
//Add a task to msg.sender
//First initialize a task
Task memory new_task = Task(false, task);
todo_list[msg.sender].push(new_task);
}
function update_task(uint idx, bool new_status) public {
todo_list[msg.sender][idx].status = new_status;
}
//T_item init_item = T_item(false);
function delete_task(uint idx) public {
delete todo_list[msg.sender][idx];
}
function get_task_count() public view returns (uint) {
return todo_list[msg.sender].length;
}
}
以及用于反应的 App.js 文件
//App.js
import React, { useState, handleSet } from "react";
import React, { useState } from "react";
import Web3 from "web3";
//import { simpleStorageAbi } from "./abi/abis";
import { Bloc } from "./abis/Bloc";
//import { abi } from "./abis/test";
import "bootstrap/dist/css/bootstrap.min.css";
import "./App.css";
const web3 = new Web3(Web3.givenProvider);
const contractAddr = "0x2891297aA45AFe375cE5dCDc87380d37BcE8eCC6";
const BlocContract = new web3.eth.Contract(Bloc, contractAddr);
const handle_read = async (e) => {
e.preventDefault();
const result = await BlocContract.methods.read_task(7).call();
console.log(result);
};
function App() {
const [number, setNumber] = useState(0);
return (
<div
className="App container"
style={{ height: "100vh", display: "flex", alignItems: "center" }}
>
<div className="container" style={{ margin: "auto" }}>
<div className="container read-task" style={{ padding: "20px" }}>
<button type="button" class="btn btn-primary" onClick={handle_read}>
Read Task
</button>
</div>
<div className="set-task" style={{ padding: "20px" }}>
<form>
<label>
Set Task
<br />
<input type="text" name="name"></input>
</label>
</form>
</div>
<div className="set-task" style={{ padding: "20px" }}>
<form>
<label>
Update Task
<br />
<input type="text" name="name"></input>
</label>
</form>
</div>
<div className="container set-idx" style={{ padding: "20px" }}>
<label>Task Index</label>
<div className="row">
<div className="col-sm">
<button
onClick={(e) =>
number > 0 ? setNumber(number - 1) : setNumber(0)
}
>
-
</button>
</div>
<div className="col">
<input type="text" name="name" value={number}></input>
</div>
<div className="col-sm">
<button onClick={(e) => setNumber(number + 1)}>+</button>
</div>
</div>
</div>
</div>
</div>
);
}
export default App;
解决方案
推荐阅读
- node.js - 打包后节点应用程序(生产和开发模式)的意外行为(npm run dist)
- rust - `&mut []` 的生命周期是否在 Rust 的生命周期分析中得到特殊处理?
- php - 消息:未定义的偏移量:1 Codeigniter
- java - 将 json 休息响应转换为数据对象 jBPM(无法将响应转换为对象)
- node.js - docker + node.js + pm2:不能在守护进程模式下运行 docker?
- python - 应用函数而不使用应用到数据框中的每一行
- javascript - 如何创建一个完美的假 jQuery Event 对象
- c# - C# 中的扩展方法
- makefile - Makefile 配方中的日期
- google-apps-script - 使用 Google Apps 脚本将图像从 Google 表格复制到 Google 文档