关系问题-Mongo集合和MeteorJS

Relationship trouble - Mongo collections and MeteorJS

本文关键字:MeteorJS 集合 -Mongo 问题 关系      更新时间:2023-09-26

我正在使用Meteor,并试图拥有一个包含产品的mongo集合和一个包含用户的mongo集。

产品有价格,但我也给了一个产品(作为目前的测试)一个"dealerPrices"子集合,其中包含这样一个对象:

 "partprice" : "98",
    "dealerPrices" : {
        "YH" : "120",
        "AB" : "125"
    },

我希望在我的网站上有一个表格,其中有一列显示"零件价格",旁边还有一列显示当前登录经销商的价格。我可以为经销商价格做一个完全独立的收集,但我不确定哪种方式更有效,因为我是Mongo的新手。

我的问题是针对字段中带有"YH"或"AB"的数字,具体取决于登录用户,Users集合有一个名为"profile"的子集合,其中有一个称为"code"的字段,该字段将与每个经销商的唯一代码"YH"或"AB"相匹配。

我使用手柄来显示Meteor中的数据,这里有一点用于显示表行的html。

Larger code section:
<template name="products">
<h2> All Products <span class="productCount"></span></h2>
<table class="table table-condensed table-striped table-hover table-bordered">
  <thead>
    <tr>
      <th class="toHide">Unique ID</th>
      <th>Print</th>
      <th>Product</th>
      <th>FF Code</th>
      <th>Base Price</th>
      <th>My Price</th>
    </tr>
  </thead>
{{> AllProducts}}
</template>

<template name='AllProducts'>
   {{#each row}}
    <tr class='productRow'>
      <td class="product-id toHide">{{_id}}</td>
      <td class="print"><input type="checkbox" class="chkPrint"/></td>
      <td class="product-name">{{partname}}</td>
      <td class="product-code">{{code}}</td>
      <td class="product-az-price">${{partprice}}</td>
      <td class="product-dealer-price">${{dealerPrices.YH}}</td>
    </tr>
   {{/each}}
</template>

我希望我能正确地解释这一点,基本上我正在努力寻找一些替代联接的方法,并在关系数据库中拥有一个产品表、一个经销商-产品-经销商-价格关系表和一个用户帐户表。

您可能希望在Template助手中完成此操作。首先,为每个循环创建一个模板,而不是仅使用{{#each}}:

<template name="fooRow">
    ... table rows
    <td class="product-dealer-price">{{userBasedThing}}</td>
</template>

然后,为这个新的fooRow模板添加一个模板助手:

Template.fooRow.userBasedThing = function () {
    var result = "";
    if (Meteor.userId() && this.dealerPrices)
        result = this.dealerPrices[Meteor.user().profile[0].code];
    return result;
}

然后去掉each循环中的内容,并将其替换为:

{{#each row}}
    {{> fooRow}}
{{/each}}

那就行了!