title: How to get the list of all the posts by a particular user in Express/Mongo?
body: <p>I have a User and Posts model, and there's a User's feed where all the posts submitted by the user will be shown. </p>
  
  <p><strong>User.js</strong></p>
  
  <pre class="lang-js prettyprint-override"><code>const mongoose = require("mongoose")
  const bcrypt = require("bcrypt")
  const Schema = mongoose.Schema
  
  const userSchema = new Schema({
      username: { type: String, required: true },
      email: { type: String, required: true },
      password: { type: String, required: true },
  }, { timestamps: true })
  
  
  userSchema.pre("save", function (next) {
      if (this.password) {
          const salt = bcrypt.genSaltSync(10)
          this.password = bcrypt.hashSync(this.password, salt)
      }
      next()
  })
  
  userSchema.methods.confirmPassword = function (password) {
      return bcrypt.compareSync(password, this.password)
  }
  
  
  const User = mongoose.model("User", userSchema)
  
  module.exports = User
  </code></pre>
  
  <p><strong>Post.js</strong></p>
  
  <pre class="lang-js prettyprint-override"><code>const mongoose = require("mongoose");
  const Schema = mongoose.Schema;
  var URLSlug = require("mongoose-slug-generator");
  
  mongoose.plugin(URLSlug);
  
  const postSchema = new Schema({
    postTitle: { type: String, required: true },
    postDescription: { type: String, required: true },
    userId: { type: Schema.Types.ObjectId, ref: "User" },
    slug: { type: String, slug: "title" }
  }, { timestamps: true }
  )
  
  postSchema.pre("save", function (next) {
    this.slug = this.postTitle.split(" ").join("-");
    next();
  });
  
  const Post = mongoose.model("post", postSchema);
  
  module.exports = post;
  </code></pre>
  
  <p>The routes look like this:</p>
  
  <p><strong>posts.js</strong></p>
  
  <pre class="lang-js prettyprint-override"><code>router.post("/new", auth.verifyToken, postsController.newposts)
  router.get("/list", postsController.listpostss)
  router.get("/:id", postsController.findposts)
  router.put("/:id/edit", postsController.updateposts)
  router.delete("/:id/delete", postsController.deleteposts)
  </code></pre>
  
  <p><strong>users.js</strong></p>
  
  <pre class="lang-js prettyprint-override"><code>router.post("/register", usersController.registerUser)
  router.post("/login", usersController.loginUser)
  router.get("/me", auth.verifyToken, usersController.identifyUser)
  router.get("/list", usersController.listUsers)
  router.get("/:id", usersController.getUser)
  router.put("/:id/edit", usersController.updateUser)
  router.delete("/:id/delete", usersController.deleteUser)
  </code></pre>
  
  <p>So, I just want the posts submitted by the user to be shown on the Feed component of my react app. </p>