title: how to find element by name using mongoose
body: <p>I am trying to get the title of an element that's already in the database. I am using <code>find()</code> to get the title but when I try to print it. I get an empty array which shouldn't be the case. here is the function that I am having a problem with. Thank you so much in advance!</p>
  <p><strong>the function</strong></p>
  <pre><code>    ...
      posts.find({}, (err, foundPosts) =&gt; {
      console.log(foundPosts[0].title);
      if (foundPosts.length !== 0) {
          if (err) {
              console.log(&quot;Error&quot;, err);
          } else {
              //i still have a problem here
              const postTitle = _.kebabCase(foundPosts[0].title);
              const postTil = _.upperFirst(postTitle);
              console.log(postTil);
              if (postTil === reqTitel) {
                  console.log(&quot;true&quot;);
                  res.render(&quot;posts&quot;, { foundPosts: foundPosts[0] });
              }
          }
      }
      });
  });
  </code></pre>
  <p><strong>Here is the entire code</strong></p>
  <p>I am only able to get the first title because I am using index[0]</p>
  <pre><code>const express = require(&quot;express&quot;);
  const bodyParser = require(&quot;body-parser&quot;);
  const ejs = require(&quot;ejs&quot;);
  const app = express();
  const _ = require(&quot;lodash&quot;);
  const mongoose = require(&quot;mongoose&quot;);
  app.set(&quot;view engine&quot;, &quot;ejs&quot;);
  app.use(express.static(__dirname + &quot;/public&quot;));
  app.use(bodyParser.urlencoded({extended: true}));
  const items = [];
  mongoose
    .connect(&quot;mongodb+srv://zaid:password@cluster0.3nbpg.mongodb.net/post&quot;, {
      useNewUrlParser: true,
      useUnifiedTopology: true,
      useUnifiedTopology: true,
    })
    .then(() =&gt; console.log(&quot;mongoDB connected&quot;))
    .catch((err) =&gt; console.log(err));
  const postsSchema = new mongoose.Schema({title: String, content: String});
  const posts = mongoose.model(&quot;post&quot;, postsSchema);
  
  app.get(&quot;/&quot;, (req, res) =&gt; {
    posts.find({}, (err, foundPosts) =&gt; {
      if (foundPosts.length !== 0) {
        if (err) {
          console.log(&quot;error line 23&quot;, err.message);
          res.render(&quot;laoding&quot;);
        } else {
          console.log(&quot;successful&quot;);
          res.render(&quot;index&quot;, {items: foundPosts});
        }
      }
    });
    // console.log(posts.find({title}));
  });
  
  app.get(&quot;/post&quot;, (req, res) =&gt; {
    res.render(&quot;post&quot;);
  });
  
  app.post(&quot;/post&quot;, (req, res) =&gt; {
    const title = req.body.title;
    const content = req.body.content;
  
    const post = new posts({
      title,
      content,
    });
    post.save();
    res.redirect(&quot;/&quot;);
  });
  
  app.get(&quot;/posts/:title&quot;, (req, res) =&gt; {
    var requstedTilte = _.kebabCase(req.params.title);
    var reqTitel = _.upperFirst(requstedTilte);
    console.log(reqTitel);
  
    posts.find({}, (err, foundPosts) =&gt; {
      console.log(foundPosts[0].title);
      if (foundPosts.length !== 0) {
        if (err) {
          console.log(&quot;Error&quot;, err);
        } else {
          //i still have a problem here
          const postTitle = _.kebabCase(foundPosts[0].title);
          const postTil = _.upperFirst(postTitle);
          console.log(postTil);
          if (postTil === reqTitel) {
            console.log(&quot;true&quot;);
            res.render(&quot;posts&quot;, {foundPosts: foundPosts[0]});
          }
        }
      }
    });
  });
  const PORT = process.env.PORT || 3000;
  
  app.listen(PORT, () =&gt; {
    console.log(`Server is listening on port ${PORT}`);
  });
  </code></pre>
  <pre><code></code></pre>