Hello im trying to get all my blogs from DB and change the author name from another collection from the database then rendering them to ejs file however the page renders before the array is filled up
app.get('/', async (req, res) => {
let blogList = new Array();
await Blog.find({}, function (err, foundBlog) {
console.log(foundBlog);
if (err) {
console.log(err);
} else {
foundBlog.forEach(async (blog) => {
await Author.findById(blog.author, async (err, author) => {
if (err) {
console.log(err);
} else {
blog.author = author.name;
console.log('this is the blogs' + blog);
blogList.push(blog);
console.log('array length 1 is ' + blogList.length);
}
});
});
console.log('array length 2 is ' + blogList.length);
console.log(blogList);
res.render('home', { blogs: blogList });
}
});
});