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>