Answer the question
In order to leave comments, you need to log in
Why is json undefined instead?
Good day. Upon receipt of json undefined, when I want to access the posts.count property, it is clear that nodemon, when updating the page, appears in the console for some time, the output of this property when reloading undefined
import axios from "axios";
import { useEffect, useState } from "react";
import Post from './Post';
import "./posts.css";
axios.defaults.baseURL = "http://localhost:5000";
//axios.defaults.headers.post['Content-Type'] ='application/json;charset=utf-8';
//axios.defaults.headers.post['Access-Control-Allow-Origin'] = '*';
export default function Posts() {
const [posts, setPosts] = useState();
useEffect(() => {
/*const apiUrl = "/api/posts";
axios.get(apiUrl).then((res) => {
const posts = res.data;
setPosts(posts);
});*/
const posts = async () => {
const res = await fetch("http://localhost:5000/api/posts");
const data = await res.json();
setPosts(data)
};
posts();
}, []);
console.log(posts.count);
return (
<div className="container">
</div>
);
}
{
"success": true,
"count": 2,
"data": [
{
"likes": 0,
"comments": 0,
"shares": 0,
"_id": "60fcbd7331a77c28a8ba6936",
"title": "My Perfect Vacations in South America and Europe",
"text": "Lorem ipsum dolor sit amet, consectadipisicing elit, sed do eiusmod por incidid ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud lorem exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis en aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt... ",
"__v": 0
},
{
"likes": 0,
"comments": 0,
"shares": 0,
"_id": "60fcbd8831a77c28a8ba6938",
"title": "Advices for Backpacking",
"text": "Lorem ipsum dolor sit amet, consectadipisicing elit, sed do eiusmod por incidid ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud lorem exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis en aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciun Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem...",
"__v": 0
}
]
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question