2014-07-17 3 views
1

제품 및 카테고리라는 두 가지 컬렉션이 있습니다. 양쪽 모두의 삽입이 잘 작동합니다. 내 문제는 제품 목록에서 카테고리의 이름을 반환하는 것입니다. 카테고리의 _id가 나열되어 있습니다. 이름이 필요해.Meteor를 사용하여 ID로 다른 콜렉션의 문서에서 문서 속성을 반환하려면 어떻게해야합니까?

Products = new Meteor.Collection('products'); 
/* 
* Add query methods like this: 
* Products.findPublic = function() { 
* return Products.find({is_public: true}); 
* } 
*/ 
var Schema = {}; 
Schema.Products = new SimpleSchema({ 
    name: { 
     type: String, 
     label: "Nome", 
     optional: false 
    }, 
    category: { 
     type: Schema.Categories, 
     optional: false 
    }, 
    "category.$.name": { 
     type: String 
    }, 
    subcategory: { 
     type: Schema.Subcategories, 
     optional: true 
    }, 
    "subcategory.$.name": { 
     type: String 
    }, 
    description: { 
     type: String, 
     label: "Descrição", 
     optional: true, 
     max: 150 
    } 
}); 
Products.attachSchema(Schema.Products); 

제품 도우미를 나열 :

Template.Products.helpers({ 
    // Return all products 
    list: function() { 
     return Products.find({}, { 
      sort: { 
       time: -1 
      }, 
      fields: { 
       name: true, 
       category: true, 
       subcategory: true 
      } 
     }).fetch(); 
    } 
}); 

그리고 제품 템플릿 : 여기

은 제품 컬렉션입니다

<template name="Products"> 
<fieldset> 
    <legend> 
     <p class="pull-left"> 
     Produtos 
     </p> 
     <a href="{{pathFor 'ProductsNew'}}" class="btn pull-right btn-success btn-sm">Novo Cadastro</a> 
     <div class="clearfix"></div> 
    </legend> 
    <table class="table"> 
     <thead> 
      <tr> 
       <th>Nome</th> 
       <th>Categoria</th> 
       <th>Subcategoria</th> 
       <th></th> 
      </tr> 
     </thead> 
     <tbody> 
      {{#each list}} 
      <tr> 
       <td>{{name}}</td> 
       <td>{{category}}</td> 
       <td>{{subcategory}}</td> 
       <td> 
        <a href="{{pathFor 'ProductsEdit' _id }}" class="btn btn-xs btn-primary">Editar</a> 
       </td> 
       <td> 
        <button class="destroy btn btn-danger btn-xs">Delete</button> 
       </td> 
      </tr> 
      {{/each}} 
     </tbody> 
    </table> 
</fieldset> 
</template> 

내가 어떻게 할 수 있습니까? 여기

내 문제의 이미지입니다 :

enter image description here

+0

내가 SimpleSchema를 못하는에서

<td>{{categoryName}}</td> <td>{{subcategoryName}}</td> 

그리고

을 시도 :하지만 내 추측이입니다. 단일 제품의 구조를 보여주는 것이 정말 도움이 될 것입니다. –

+0

스크린 샷이 필요합니까? – marcodamaceno

답변

1

다윗이 권리입니다. 하나의 제품 예가 도움이 될 것입니다. 당신이하려는 것을 이해하는 것은 어렵습니다. 템플릿에서

당신의 JS

Template.Products.categoryName = function() { 
     var _category = Categories.findOne({_id: this.category}); 
     return _category ? _category.name : ""; 
} 

Template.Products.subcategoryName = function() { 
     var _subcategory = Subcategories.findOne({_id: this.subcategory}); 
     return _subcategory ? _subcategory.name : ""; 
} 
+0

그게 다야! 그것은 작동합니다! 고맙습니다! – marcodamaceno