0
I’m following a tutorial on how to make an instagram, has been done and tested the backend and frontend separately. In the backend I can change the routes and send/save information to Mongodb, in the frontend I can display the information the way I want. The problem is when I go to pull from the database, I made a scheme for it to go through all the database posts displaying its information, only apparently it is not going through any position. I don’t think the problem is mongodb because I can connect and upfile (but not get them?).
I’m sure the code is as in the video.
import React, {Component} from 'react';
import api from '../services/api';
import './Feed.css';
import more from '../assets/more.png';
import tolike from '../assets/tolike.png';
import nolike from '../assets/nolike.png';
import comment from '../assets/comment.png';
import send from '../assets/send.png';
class Feed extends Component{
state = {
feed: [],
};
async componetDidMount() {
const response = await api.get('posts');
this.setState({ feed: response.data });
}
render() {
return(
<section id="post-list">
{ this.state.feed.map(post => (
<article>
<header>
<div className="user-info">
<span>{post.author}</span>
<span className="place"> {post.place}</span>
</div>
<img src={more} alt= "Mais"/>
</header>
<img src= {`http://localhost:3333/files/${post.image}`} alt= "post"/>
<footer>
<div className= "actions">
<img src = {nolike} alt=""/>
<img src = {comment} alt=""/>
<img src = {send} alt=""/>
</div>
<strong>{post.likes}</strong>
<p>
{post.description}
<span> {post.hashtag}</span>
</p>
</footer>
</article>
))}
</section >
);
}
}
export default Feed;
I’ll leave the project as an attachment, I’m sure it’s easy for anyone who understands it. My studies with web programming are locked because of this Firefoxsend download backend.rar and frontend.rar
Following recommendations I used the console.log(Response.data) but did not get error alerts either in the browser console or in vs code.
async componetDidMount() {
const response = await api.get('posts');
this.setState({ feed: response.data });
console.log(response.data);
}
The description is very vague. Have you tried tracking where this problem starts? Have you checked if you are receiving data from the server?
componetDidMount
you make aconsole.log(response.data)
, what the console prints?– Andre
tested as above, did not get error alerts which I think is a good sign
– Joanderson Pereira
But what appears on
console.log
? Does the populated array appear as it should? Does an empty array appear? Appearsundefined
?– Andre
That was the problem, literally nothing was appearing on the console. I realized that componetDidMount is a nonexistent component of React, meaning it wasn’t accomplishing anything. The correct is componentDidMount, now the page displays the information I want and in the console.log appears the populated array. The problem was just a typo that wouldn’t allow me to use the React resource. I lost two weeks because of an "n" before the "t".
– Joanderson Pereira