Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

276
Views
how can i render multiple collections from mongodb in a single page

it is my news article type blog website where i want to post news in form of cards and breaking news as a heading and change it on daily basis using mongodb. so, first i render posts from mongodb in my project it runs perfectly but when i want another collection so that i can post headings like breaking news it shows error like headings is not defined. But if i remove the other collection i.e. the post part it runs perfectly. problem is both post and breaking news heading part is not working in same page. How can i do that?

app.js

const postSchema = {
  title: String,
  content: String
};
const headSchema = {
  content: String
};

const Post = mongoose.model("Post", postSchema);
const Heading = mongoose.model("Heading",headSchema);


app.get("/",function(req,res){
    Post.find({}, function(err, posts){
        res.render("home" , {
          posts: posts
        });
      });

});
app.get("/",function(req,res){
Heading.find({},function(err,headings){
  res.render("home", {
    headings: headings
  });
});
});

home.ejs

  <div class="heading">
    <img class="news" src="/images/news.png" alt="">
    <div class="headingtext">
      <h1>Today's Heading</h1>
      <% headings.forEach(function(heading){ %>
      <p><%= heading.content %> </p>
      <% }) %>
    </div>
  </div>



  <div class="cardsec">

    <% posts.forEach(function(post){ %>

      <div class="card">
            <img class="mag" src="/images/mag.png" alt="">
            <div class="cardint">
              <img class="cardimg" src="/images/harappa.jpg" alt="harappa">
              <div class="cardtxt">
                <h3><%= post.title %></h3>
                <p>  <%= post.content.substring(0, 200) + "..." %>
                  <a href="/posts/<%= post._id %>">Read More</a>
                 </p>
              </div>
            </div>
          </div>



    <% }) %>
    </div>

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!