I am trying out my first Solidity project; a todo app. I have worked on the smart contract using remix and tested all its functions, which work as intended. I worked on my frontend and connected it using ethers.js and functions like addTodo
, clearAllTasks
work perfectly well except the function to get all the tasks.
This is the error I keep getting:
index.js:160 Error fetching todos: Error: call revert exception (method="getTodos()", errorArgs=null, errorName=null, errorSignature=null, reason=null, code=CALL_EXCEPTION, version=abi/5.2.0)
and this is the code I run to get all the tasks:
async function getTasks() {
try {
const todos = await TodoContract.getTodos();
console.log("Todos:", todos);
return todos;
} catch (error) {
console.error("Error fetching todos:", error);
}
}
Can someone please help me out?
here is the smart contract
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.8;
contract Todo{
struct TodoItem {
string task;
bool completed;
}
TodoItem[] public todoList;
function addTodo (string memory task) public {
todoList.push(TodoItem(task, false));
}
function getTodos () public view returns(TodoItem[] memory){
return todoList; }
function updateTodo(uint256 index) public {
require(index < todoList.length, "index out of range");
todoList[index].completed = true;
}
function deleteTodo (uint256 index) public {
require(index < todoList.length, "index out of range"); todoList[index] = todoList[todoList.length - 1]; todoList.pop(); }
function clearAll () public {
delete todoList;
}
function editTodo (uint256 index, string memory updateValue) public {
require(!todoList[index].completed, "todo already completed, set another todo"); todoList[index].task = updateValue; } }
the abi and smart contract address are correct as other parts of the smart contract are interacting properly.
here is a part of my abi
{
inputs: [],
name: "getTodos",
outputs: [
{
components: [
{
internalType: "string",
name: "task",
type: "string",
},
{
internalType: "bool",
name: "completed",
type: "bool",
},
],
internalType: "struct Todo.TodoItem[]",
name: "",
type: "tuple[]",
},
],
stateMutability: "view",
type: "function",
},