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

260
Views
How to take only articles by given user Express.js

I want to display in my user/myarticlesview only articles by logged in user. How can i do that: Here is my User model and Article schema:

let userSchema = mongoose.Schema(
{
    email: {type: String, required: true, unique: true},
    passwordHash: {type: String, required: true},
    salt: {type: String, required: true},
    articles: [{type: ObjectId, ref: 'Article'}],
    roles: [{type: ObjectId, ref: 'Role'}]
}

);

let articleSchema = mongoose.Schema (
{
    author: {type: ObjectId, ref: 'User'},
    title: {type: String, required: true },
    content: {type: String, required: true },
    phone: {type: Number, required: true },
    date: {type: Date, default: Date.now() }
}

);

I want to do this in my userController and passed it to the view:

myArticlesGet: (req, res) => {
    if (!req.isAuthenticated()) {
        res.redirect('/');
        return;
    }

    res.render('user/myarticles')

}

I cant figure it out how to make the query.Thank you.

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

As you are using express sessions you can store userId in the express session when the user is authenticated and then you can get user articles from user like that

User.find({_id: req.session.userId}).populate('articles')
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!