populate in mongodb with meteor

前端 未结 1 1207
北海茫月
北海茫月 2021-01-13 14:22

I am using meteor.js. I am having three collections boards,categories,users.

Boards : 
 {
 \"_id\": ObjectId(\"su873u498i0900909sd\"),
  \"locked\": NumberIn         


        
相关标签:
1条回答
  • 2021-01-13 15:21

    Probably collection helpers.

    Basic usage:

    Boards.helpers({
      creator: function () {
        return Meteor.users.findOne(this.creatorId);
      },
      category: function () {
        return Categories.findOne(this.categoryId);
      }
    });
    

    Usage in template is pretty simple. Let's say you have your board:

    {{#each boards}}
      <div>
        <h3>{{board_name}}</h3>
        <p>Created by</p>: {{ creator.username }}
        <p>Category</p>: {{ category.catname }}
      </div>
    {{/each}}
    

    Added tip: use publish-composite to publish the relationships in a more manageable fashion.

    Meteor.publishComposite('board', function (boardId) {
      check(boardId, String);
      return {
        find: function () {
          return Boards.find(boardId);
        },
        children: [{
          find: function (board) {
            return Meteor.users.find(board.creatorId);
          }
        }, {
          find: function (board) {
            return Categories.find(board.categoryId);
          }
        }]
      }
    });
    
    0 讨论(0)
提交回复
热议问题