Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

162
Views
Mongoose referring to Comment and to it child Users

I'm working on my personal project which is just simple blog and I got stuck with this problem:

I have 3 Mongoose Schemas:

Blog:

var blogSchema = new mongoose.Schema({
    title: String,
    image: String,
    description: String,
    body: String,
    created: { type: Date, default: Date.now() },
    comments: [
        {
            type: mongoose.Schema.Types.ObjectId,
            ref: "Comment"
        }
    ]
});

Comment:

var commentSchema = new mongoose.Schema({
    text: String,
    author_id: {
        type: mongoose.Schema.Types.ObjectId,
        ref: "User"
    }
});

User:

var userSchema = new mongoose.Schema({
    username: String,
    password: String,
    avatar_url: String,
    email: String
});

And in each blog post I'm trying to display comments from array which is working correctly but then I don't know how to access to User model to display usernames and avatar_urls

app.get("/blogs/:id",function(req,res){
    Blog.findById(req.params.id).populate("comments").populate("author_id").exec(function(err,findBlog){
        if(err){
            res.redirect("back");

            console.log(err);
        }else{
            res.render("show" , {blog: findBlog});
        }
    })
})
about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

you have to declare user information like username in the blog schema then only you can send request to userschema for avatar_url

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!