Node.js 如何仅获取给定用户Express.js的文章

Node.js 如何仅获取给定用户Express.js的文章,node.js,mongodb,express,mongoose,Node.js,Mongodb,Express,Mongoose,我想在我的用户/myarticlesview中仅显示登录用户的文章。 我如何做到这一点: 以下是我的用户模型和文章模式: let userSchema = mongoose.Schema( { email: {type: String, required: true, unique: true}, passwordHash: {type: String, required: true}, salt: {type: String, required: true},

我想在我的用户/myarticlesview中仅显示登录用户的文章。 我如何做到这一点: 以下是我的用户模型和文章模式:

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'}]
}
))

))

我想在userController中执行此操作,并将其传递给视图:

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

    res.render('user/myarticles')

}

我不知道如何进行查询。谢谢。

因为您正在使用
快速会话
,当用户通过身份验证后,您可以在快速会话中存储
用户ID
,然后您可以像这样从用户处获取用户文章

User.find({_id: req.session.userId}).populate('articles')

您正在使用吗?是的,我正在使用express Sessions从会话中查找用户文档。获得用户文档后,只需
文章
User.find({_id: req.session.userId}).populate('articles')