0

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 });
        }
      });
    });
1
  • Try replacing your code with this code Commented Mar 1, 2022 at 10:09

2 Answers 2

1

Please use ref of Author in Blog. In other words make a relation of blog with author. And then your backend should be as follows:

app.get('/', async (req, res) => {
      
         const blogs=await Blog.find({}).populate('author').exec(); // field name author is being populated.
      
          res.render('home', { blogs });
        }
    
    );

const BlogSchema=new mongoose.Schema({
 ... rest of fields,
 author:{
  type:ObjectId,
  ref:"Author" // here it is model name 
 }
});

With this code now your whole author object will be embedded in each blog like:

blog={
  name:"",
  author:{
    name:"Abc",
  }
}

And you can easily get author details by accessing blog.author.name.

Sign up to request clarification or add additional context in comments.

2 Comments

Note to OP: this is the better approach as compared to what you are doing. Author document should be nested inside the blog document. This way, you don't need to query the Author collection. In fact, you can just get rid of the Author collection.
so ill nest the author object inside the blog collection... thank you sir for the help !
0

for your query, you can explore populate in your Mongo Schema in the collection.

const BlogSchema = new mongoose.Schema({
 author:{
  type:ObjectId,
  ref:"Author" //here it is model name 
 }
})
const Blog = mongoose.model("Blog", BlogSchema )

const AutherSchema = new mongoose.Schema({
 _id:{
  type:ObjectId
 }
})
const Auther = mongoose.model("Auther", AutherSchema)

 app.get('/', async (req, res) => {
      
 const blogs = await Blog.find({}).populate('author').exec(); // field name author is being populated.
         res.send(blogs)
        }
    )

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.