Heidi
Heidi

Reputation: 41

Cannot reach "title" property in an Object which has this property

When I try access "title" and "body" on "post" object it says that "Cannot find name 'title'." I tried everything that I found here on stackoverflow, my code seems to be the same as the other ones, but I cannot figure out why it still not working. I would appreciate if someone could find the problem and explain to a totally beginner. Thank you! :)

const PostDetail = () => {
const { id } = useParams();

const [post, setPost] = useState({});

const getPost = async () => {
    try {
        const { data } = await axios.get(
            `https://jsonplaceholder.typicode.com/posts/${id}`
        );

        setPost(data);
        console.log(data);
        
    } catch (err) {
        console.log(err);
    }
};

useEffect(() => {
    getPost();
}, []);

return (
    <>
        <Header />
        <Navigator />
        <div className="App">
            <h3>Post details: {id}</h3>
            <p>{title}</p>
            <p>{body}</p>
        </div>
        <Footer />
    </>
);

};

export default PostDetail;

The error:

ERROR in src/pages/PostDetail.tsx:38:9

TS2304: Cannot find name 'title'. 36 | 37 |

Post details: {id}

38 |

{title}

/// Cannot find name 'title'. | ^^^^^ 39 |

{body}

/// Cannot find name 'body'. 40 | 41 |

ERROR in src/pages/PostDetail.tsx:39:9 TS2304: Cannot find name 'body'. 37 |

Post details: {id}

38 |

{title}

/// Cannot find name 'title'.

39 |

{body}

/// Cannot find name 'body'. | ^^^^ 40 | 41 | 42 | </>

Upvotes: 0

Views: 40

Answers (1)

TalOrlanczyk
TalOrlanczyk

Reputation: 1213

I don't see it as a variable you save data on. maybe you need to access it from the post object? I say it because I see only Post state and no title state.

Update I mean you save data in post but title and body cant know that you need to write post.title because title is just a variable he can't know what post child until you state so

Upvotes: -1

Related Questions