martinii
martinii

Reputation: 141

Filtering Posts by User Object Id or Author in Keystone

I'm trying to create an web app where a student can store their notes online. So, if they login they can only see the notes that they posted on the blog.

    view.on('init', function (next) {
    Post.model.findOne()
          .where('_id', User.id)
          .populate('author')
          .exec(function(err, results) {
              locals.data.posts = results;
      });
});

this is the code that I am having problems on

Upvotes: 1

Views: 349

Answers (1)

martinii
martinii

Reputation: 141

    view.on('init', function (next) {
    var q = keystone.list('Post').paginate({
        page: req.query.page || 1,
        perPage: 10,
        maxPages: 10,
    })
        .find().where('author', locals.user.id)
        .sort('-publishedDate')
        .populate('author categories');

    if (locals.data.category) {
        q.where('categories').in([locals.data.category]);
    }

    q.exec(function (err, results) {
        locals.data.posts = results;
        next(err);
    });
});

This works!

Upvotes: 1

Related Questions