javascript - 如何使用 Meteor 按 ID 从另一个集合中的文档返回文档属性?

标签 javascript mongodb meteor spacebars simple-schema

我有两个集合:产品和类别。两者的插入工作正常。我的问题是返回产品列表中的类别名称。它列出了类别中的 _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

最佳答案

大卫是对的。单个产品示例会很有帮助。很难理解你想做什么。但我最好的猜测是:

在你的模板中尝试

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

在你的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 : "";
}

关于javascript - 如何使用 Meteor 按 ID 从另一个集合中的文档返回文档属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24811376/

相关文章:

java - MongoDB with Java - 查找文档并嵌套插入更新

java - 无法插入新创建的 mongolab 数据库

javascript - 使用 Meteor HTTP 在 Spotify API 上请求 access_token 时出现不支持的授权类型错误

javascript - 在动态标记上使用事件

javascript - 从 Javascript 向 Servlet 发送数据

javascript - Windows 边栏小工具中的通知/警报

javascript - 检查不同服务器上的文件

javascript - 我如何根据用户在复选框中的选择执行特定的 css

javascript - meteor 中的相关项目

mongodb - Mongo Shell和代码结果返回不同的结果