I am using PERN Stack with bootstrap for styling. So, I want to access my JSON data and show it in the form of todo lists. When I tried using map function I got TypeError:
Here is the code:
JavaScript
x
40
40
1
import React, { useState, useEffect } from "react";
2
3
const ListToDo = () => {
4
const [todos, setTodos] = useState([]);
5
6
const getTodos = async () => {
7
try {
8
const res = await fetch("http://localhost:3000/todos");
9
const jsonData = await res.json();
10
console.log(jsonData.data.todos);
11
setTodos(jsonData.data.todos);
12
} catch (err) {
13
console.log("ERROR: ", err);
14
}
15
};
16
17
useEffect(() => {
18
getTodos();
19
}, []);
20
21
return (
22
<>
23
<ul className="list-group my-4 ">
24
{todos.map((todo) => (
25
<li className="list-group-item d-flex justify-content-between align-items-center">
26
{todo.description}
27
<span className="">
28
<button className="btn btn-dark ">Edit</button>
29
<span> </span>
30
<button className="btn btn-dark ">Delete</button>
31
</span>
32
</li>
33
))}
34
</ul>
35
</>
36
);
37
};
38
39
export default ListToDo;
40
Advertisement
Answer
I misspelled my variable. So, the problem was I was writing jsonData.data.todos
instead off jsonData.data.toDos
.