I am trying to display some entries from the backend. If I pass data through Postman, the data is perfectly passed to the database. However, I'm unable to display them in the frontend. Here's my code
export default function EntriesDisplay() {
const [entry,setEntry] = useState([]);
const [update, setUpdate] = useState(false);
useEffect(function() {
fetch("http://localhost:8000/api/entries")
.then((res) => {
console.log(res.data);
setEntry(res.data)
})
.catch((err)=>{
console.log(err);
})
}, [update])
return(
<>
<ul className="list-container">
{entry.map((data) => (
<EntriesCard
data={data}
handleEdit={handleEdit}
handleDelete={handleDelete}
/>
))}
</ul>
Here's the component EntriesCard
function EntriesCard({data, handleEdit, handleDelete}) {
const{_id, title, link, description} = data;
return(
<li key={_id}>
<div className="title-description">
<h3>{title}</h3>
<h2>{link}</h2>
<p>{description}</p>
</div>
<div className="button-container">
<button className="button" name={_id} onClick={handleEdit}>
Edit
</button>
<button className="button" name={_id} onClick={handleDelete}>
Delete
</button>
</div>
</li>
)
}
Here's the backend of the code
app.get('/api/entries', asyncHandler(async (req, res) => {
const entries = await Entry.find();
res.json(entries);
})
)