Tuesday, 2 May 2017

Expressjs. How to add to views two collections from MongoDB with the coincidence of the fields id

I learning NodeJS and try to use Monk with MongoDB to make a blog. I have MongoDB with two collections: Posts and Categories. In Posts collection i have field named as Category that contain IDs of Categories that are used in docs. I want to render 2-5 posts on page. Into every post i show post data, but i need to get Names and Urls from Categories collection. How i can do multiple request to get right views of posts?

// BAD CODE WITH ERROR !!!!
router.get('/', function(req, res, next) {
  var allPosts = db.get('posts');
  allPosts.find({},['url','h1','title','description','category','author','updatedate','postimage'],function(err,posts){
    var singlecat = db.get('categories');
    singlecat.find({'_id':{$in:post.category}},['name','lat'],function(err,artCat){
      res.render('index', {
                          artCat: artCat,
                          posts: posts,
                          title: 'page 1'
                          });
    });
  });
});

Thanks!



via Nikita M

No comments:

Post a Comment