title: How to get posts by userID?
body: <p>I am working on a project which demands to show posts of a particular user when a guest or a logged in user clicks on their name, which passes the user's id (whose posts anybody wants to see) to the URL parameters. I have tried to get the user_id from the URL and pass it to the functions which returns all the posts posted by the user. </p>
  
  <p>I am using the MERN stack, and I am sorry for asking such a silly question but, I am new to this technology.</p>
  
  <p>Here is the route for getting the posts:</p>
  
  <pre><code>// @route   GET api/posts/posted/:id
  // @desc    Get posts by ID
  // @access  Public
  router.get("posted/:user", (req, res) =&gt; {
    Post.find({ user: req.users._id })
      .sort({ date: -1 })
      .then(posts =&gt; res.json(posts))
      .catch(err =&gt;
        res.status(404).json({ nopostfound: "No post found with that ID" })
      );
  });
  </code></pre>
  
  <p>Here is the action, which is called when the URL is accessed:</p>
  
  <pre><code>// Get Posts by userId
  export const getUserPosts = user =&gt; dispatch =&gt; {
    dispatch(setPostLoading());
    axios
      .get(`/api/posts/posted/${user}`)
      .then(res =&gt;
        dispatch({
          type: GET_POSTS,
          payload: res.data
        })
      )
      .catch(err =&gt;
        dispatch({
          type: GET_POSTS,
          payload: null
        })
      );
  };
  </code></pre>
  
  <p>This is the code sample for showing the posts</p>
  
  <pre><code>import React, { Component } from "react";
  import PropTypes from "prop-types";
  import { connect } from "react-redux";
  import PostedFeed from "./PostedFeed";
  import Spinner from "../common/Spinner";
  import { getUserPosts } from "../../actions/postActions";
  
  class MyPosts extends Component {
    componentDidMount() {
      this.props.getUserPosts(this.props.match.params);
    }
    render() {
      const { posts, loading } = this.props.post;
  
      let postedContent;
  
      if (posts === null || loading) {
        postedContent = &lt;Spinner /&gt;;
      } else {
        postedContent = &lt;PostedFeed posts={posts} /&gt;;
      }
      return (
        &lt;div className="marginContainer"&gt;
          &lt;div className="container"&gt;
            &lt;div className="row"&gt;
              &lt;div className="col-md-12"&gt;{postedContent}&lt;/div&gt;
            &lt;/div&gt;
          &lt;/div&gt;
        &lt;/div&gt;
      );
    }
  }
  
  MyPosts.propTypes = {
    getUserPosts: PropTypes.func.isRequired,
    post: PropTypes.object.isRequired
  };
  
  const mapStateToProps = state =&gt; ({
    post: state.post
  });
  
  export default connect(
    mapStateToProps,
    { getUserPosts }
  )(MyPosts);
  </code></pre>
  
  <p>This is the PostedFeed code:</p>
  
  <pre><code>import React, { Component } from "react";
  import PropTypes from "prop-types";
  import PostedItem from "./PostedItem";
  
  class PostedFeed extends Component {
    render() {
      const { posts } = this.props;
  
      return posts.map(post =&gt; &lt;PostedItem key={post._id} post={post} /&gt;);
    }
  }
  
  PostedFeed.propTypes = {
    posts: PropTypes.array.isRequired
  };
  
  export default PostedFeed;
  </code></pre>
  
  <p>And here is the post's skeleton, which is passed to the MyPost through PostedFeed:</p>
  
  <pre><code>import React, { Component } from "react";
  import PropTypes from "prop-types";
  import { connect } from "react-redux";
  import classnames from "classnames";
  import { Link } from "react-router-dom";
  import Moment from "react-moment";
  import { addLike, removeLike } from "../../actions/postActions";
  
  class PostedItem extends Component {
    onLikeClick(id) {
      this.props.addLike(id);
    }
  
    onUnlikeClick(id) {
      this.props.removeLike(id);
    }
  
    findUserLike(likes) {
      const { auth } = this.props;
      if (likes.filter(like =&gt; like.user === auth.user.id).length &gt; 0) {
        return true;
      } else {
        return false;
      }
    }
  
    render() {
      const { post, showActions } = this.props;
  
      return (
        &lt;div className="card mb-3"&gt;
          &lt;div className="row"&gt;
            &lt;div className="col-md-3 text-center m-auto px-0 py-3"&gt;
              &lt;img src={post.avatar} alt={post.designation} /&gt;
            &lt;/div&gt;
            &lt;div className="col-md-9 m-auto p-0"&gt;
              &lt;div className="card-body m-2"&gt;
                &lt;h2 className="card-title"&gt;
                  {post.designation} - {post.location}
                &lt;/h2&gt;
                &lt;div className="mx-2"&gt;
                  &lt;ul className="list-unstyled"&gt;
                    &lt;li&gt;
                      &lt;b&gt;Date Posted:&amp;nbsp;&lt;/b&gt;
                      &lt;Moment format="DD/MM/YYYY"&gt;{post.created}&lt;/Moment&gt;
                    &lt;/li&gt;
                    &lt;li&gt;
                      &lt;b&gt;Salary:&amp;nbsp;&lt;/b&gt;₹{post.salary / 100000}&amp;nbsp;lacs/annum
                    &lt;/li&gt;
                    &lt;li&gt;
                      &lt;b&gt;Experience Required:&amp;nbsp;&lt;/b&gt;
                      {post.experience} years
                    &lt;/li&gt;
                  &lt;/ul&gt;
                  &lt;p className="card-text"&gt;{post.description}&lt;/p&gt;
                &lt;/div&gt;
              &lt;/div&gt;
              &lt;div className="card-footer mx-3 p-2"&gt;
                &lt;div className="row px-3"&gt;
                  {showActions ? (
                    &lt;span&gt;
                      &lt;button
                        onClick={this.onLikeClick.bind(this, post._id)}
                        type="button"
                        className="btn btn-light mr-1"
                      &gt;
                        &lt;i
                          className={classnames("fas fa-thumbs-up", {
                            "text-info": this.findUserLike(post.likes)
                          })}
                        /&gt;
                        &lt;span className="badge badge-light"&gt;
                          {post.likes.length}
                        &lt;/span&gt;
                      &lt;/button&gt;
                      &lt;button
                        onClick={this.onUnlikeClick.bind(this, post._id)}
                        type="button"
                        className="btn btn-light mr-1"
                      &gt;
                        &lt;i className="text-secondary fas fa-thumbs-down" /&gt;
                      &lt;/button&gt;
                    &lt;/span&gt;
                  ) : null}
                  &lt;Link to={`/post/${post._id}`} className="btn btn-info ml-auto"&gt;
                    View Details
                  &lt;/Link&gt;
                &lt;/div&gt;
              &lt;/div&gt;
            &lt;/div&gt;
          &lt;/div&gt;
        &lt;/div&gt;
      );
    }
  }
  
  PostedItem.defaultProps = {
    showActions: true
  };
  
  PostedItem.propTypes = {
    addLike: PropTypes.func.isRequired,
    removeLike: PropTypes.func.isRequired,
    post: PropTypes.object.isRequired,
    auth: PropTypes.object.isRequired
  };
  
  const mapStateToProps = state =&gt; ({
    auth: state.auth
  });
  
  export default connect(
    mapStateToProps,
    { addLike, removeLike }
  )(PostedItem);
  </code></pre>
  
  <p>Now tell me what should I do in order to populate posts to the page?</p>