Company logo
  • Empleos
  • Bootcamp
  • Acerca de nosotros
  • Para profesionales
    • Inicio
    • Empleos
    • Cursos y retos
    • Preguntas
    • Profesores
    • Bootcamp
  • Para empresas
    • Inicio
    • Nuestro proceso
    • Planes
    • Pruebas
    • Nómina
    • Blog
    • Calculadora

0

85
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>

7 months 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 empleo Planes Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2023 PeakU Inc. All Rights Reserved.