Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

278
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda