【问题标题】:Meteor subscription _id through arrayMeteor 通过数组订阅_id
【发布时间】:2015-10-03 16:40:14
【问题描述】:

我现在正在做一个清单。

用户将能够搜索然后单击搜索的项目以将相应的 id 添加到他们的列表中。

这是我编写的以下代码。

Template.searchedItem.events

'click .addToList': function (event, template) {
    event.preventDefault();

    // _id of the List
    var listId = Template.parentData(1)._id;
    console.log(listId);

    // _id of the searched item
    var companyId = this._id;
    console.log(companyId);

    Meteor.call('addToList', listId, companyId, function (error, result) {
     if (error) {

     } else {

     }
    });
  }

方法

Meteor.methods({
  addToList: function (listId, companyId) {
    check(listId, String);
    check(companyId, String);

    var add = Lists.update({
      _id: listId,
      companies: {$ne: companyId}
    }, {
      $addToSet: {companies: companyId},
      $inc: {companyCount: 1}
    });

    return add;
  }
});

列表对象

list = {
  _id: listId,
  name: nameOfList
  companies: [
    // _ids of added companies
    0: 'addedCompanyId',
    1: 'someOtherAddedCompanyId',
    and so forth...
  ]
}

从这里开始,我正在尝试创建一个子/pub,仅显示在 list.companies 中添加的公司。

我不知道如何..

我正在考虑这样的事情。

<template name = "list">
  {{#each addedCompanies}}
    something here
  {{/each}}
</template>

Template.list.helpers({
  addedCompanies: function () {
   companies = this.companies;
   return Clients.find(/*something here*/);
  }
});

到目前为止,我所写的所有内容都有效。但是我在从_ids 的数组中返回一个游标时迷失了。

更新

简单地说,我可以从多个 _id 中返回一个游标作为查询吗??

【问题讨论】:

    标签: meteor publish-subscribe meteor-helper


    【解决方案1】:

    是的,您可以从 id 列表中返回游标。您甚至可以在任何有效查询上返回光标。

    Meteor.publish('clientsFromCompanyIds', function(companyIds) {
      return Clients.find({companyId: {$in: companyIds}});
    });
    

    这就是你要找的吗?

    【讨论】:

    • 除了$in还有哪些运算符?我想发布所有值小于某个值的项目。 :)
    • 哦,抱歉发现它更小是 $lt
    猜你喜欢
    • 2014-10-10
    • 1970-01-01
    • 1970-01-01
    • 2017-06-07
    • 1970-01-01
    • 2013-01-18
    • 2015-04-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多