title: Why is find({}) returning undefined?
body: <p>Hi I am making a basic social application that as of right now has an <code>Account</code> and a <code>Post</code> model.</p>
  <p>I am able to display the <code>Account</code> model plus more, but I am having issues with the <code>Post</code> model specifically when I go to display all the posts.</p>
  <p>The mongoose function <code>Post.find({}).exec(function(err, posts)</code> in <code>allPosts.js</code> keeps returning <code>undefined</code> even when I have posts in the database.</p>
  <p>NOTE: I can successfully post to the <code>posts</code> collection just cant read from them.</p>
  <p>I am able to display all accounts but not all posts.</p>
  <hr />
  <p>Update: I can now create a post then get redirected back to the <code>allPosts.pug</code> page and see all posts except for the post that was just created, take a look at <code>addNewPost.js</code>.</p>
  <p>I would like to be able to go from my login page and have all the post displayed check out <code>index.js</code></p>
  <pre><code>/* Account.js */
  
  const mongoose = require(&quot;mongoose&quot;)
  
  const Account = mongoose.model(&quot;Account&quot;, {
      name: String,
      lastName: String,
      email: {type: String, unique: true, lowercase: true, required: true},
      phoneNumber: String,
      password: String
  })
  
  module.exports = Account
  </code></pre>
  
  <pre><code>/* Post.js */
  
  const mongoose = require(&quot;mongoose&quot;)
  
  const Post = mongoose.model(&quot;Post&quot;, {
      emailId: {type: String, required: true},
      postTitle: {type: String, required: true},
      postMessage: {type: String, required: true},
      datePosted: {type: Date, default: Date.now},
  })
  
  module.exports = Post
  </code></pre>
  
  <pre><code>/* allPosts.js */
  
  var express = require('express');
  var router = express.Router();
  const Account = require(&quot;../models/account&quot;)
  const Post = require(&quot;../models/post&quot;)
  
  /* GET all posts page. */
  router.get('/', function(req, res, next) {
      Post.find({}).exec(function(err, posts) {
          res.render(&quot;allPosts&quot;, {title: &quot;Social Application&quot;, posts: posts})
      })
  })
  
  module.exports = router;
  </code></pre>
  
  <pre><code>/* allPosts.pug */
  
  extends layout 
  
  block content 
      h1 #{title}
  
      if posts === undefined
          h1 No posts to show here 
      else 
          each post in posts
              div(class=&quot;container&quot;)
                  div(class=&quot;jumbotron&quot;)
                      h1= post.postTitle 
                      h1= post.postMessage
      br
      
      a(href=&quot;/addNewPost&quot;): button(class=&quot;btn btn-primary float-end&quot;) Add post
      a(href=&quot;/logout&quot;): button(class=&quot;btn btn-primary float-end&quot;) Logout
  </code></pre>
  
  <pre><code>/* addNewPost.js */
  
  var express = require('express');
  const Post = require('../models/post');
  var router = express.Router();
  
  /* GET all posts page. */
  router.get('/', function(req, res, next) {
      res.render(&quot;addNewPost&quot;, {title: &quot;Social Application&quot;})
  });
  
  router.post('/', function(req, res, next) {
      var emailId = req.session.email
      var postTitle = req.body.postTitle
      var postMessage = req.body.postMessage
  
      var postData = {
          emailId: emailId,
          postTitle: postTitle,
          postMessage: postMessage
      }
  
      var aPost = new Post(postData)
      aPost.save(function(err) {
          if (err) {
              console.log(err)
          } else {
              console.log(&quot;Post saved successfully !&quot;)
          }
      })
  
      Post.find({}).exec(function(err, posts) {
          res.render(&quot;allPosts&quot;, {title: &quot;All posts&quot;, posts: posts})
      })
  })
  
  module.exports = router;
  </code></pre>
  
  <pre><code>/* index.js */
  
  var express = require('express');
  var router = express.Router();
  const Account = require(&quot;../models/account&quot;)
  const bcrypt = require(&quot;bcrypt&quot;);
  
  /* GET home page. */
  router.get('/', function(req, res, next) {
    res.render('index', { title: &quot;Social Application&quot; });
  });
  
  /* POST home page */
  router.post('/', function(req, res, next) {
    var email = req.body.email
    var password = req.body.password
  
    Account.findOne({email:email}).exec(function(err, account) {
      if (account) {
        if (bcrypt.compareSync(password, account.password)) {
          req.session.email = account.email
          req.session.userLoggedIn = true
          res.render(&quot;allPosts&quot;, {title: &quot;All Posts&quot;})
        } else {
          res.render(&quot;index&quot;, {error: &quot;Your credentials are incorrect !&quot;, title: &quot;Social Application&quot;})
        }
      } else {
        res.render(&quot;index&quot;, {error: &quot;This account does not exist !&quot;, title: &quot;Social Application&quot;})
      }
    })
  })
  
  module.exports = router;
  </code></pre>
  
  <pre><code>/* Trace */
  
  TypeError: C:\Users\bmxfi\Desktop\code\NodeJavascript\SocialApplication\SocialApplication\views\allPosts.pug:7
      5| 
      6|     
    &gt; 7|     each post in posts
      8|         div(class=&quot;container&quot;)
      9|             div(class=&quot;jumbotron&quot;)
      10|                 h1= post.postTitle 
  
  Cannot read property 'length' of undefined
      at eval (eval at wrap (C:\Users\bmxfi\node_modules\pug\node_modules\pug-runtime\wrap.js:6:10), &lt;anonymous&gt;:36:32)
      at eval (eval at wrap (C:\Users\bmxfi\node_modules\pug\node_modules\pug-runtime\wrap.js:6:10), &lt;anonymous&gt;:71:4)
      at template (eval at wrap (C:\Users\bmxfi\node_modules\pug\node_modules\pug-runtime\wrap.js:6:10), &lt;anonymous&gt;:93:7)
      at Object.exports.renderFile (C:\Users\bmxfi\node_modules\pug\lib\index.js:454:38)
      at Object.exports.renderFile (C:\Users\bmxfi\node_modules\pug\lib\index.js:444:21)
      at View.exports.__express [as engine] (C:\Users\bmxfi\node_modules\pug\lib\index.js:493:11)
      at View.render (C:\Users\bmxfi\node_modules\express\lib\view.js:135:8)
      at tryRender (C:\Users\bmxfi\node_modules\express\lib\application.js:640:10)
      at Function.render (C:\Users\bmxfi\node_modules\express\lib\application.js:592:3)
      at ServerResponse.render (C:\Users\bmxfi\node_modules\express\lib\response.js:1012:7)
  </code></pre>