Search code examples
meteorpublish-subscribemeteor-helper

Meteor subscription _id through array


I am making a list right now.

Users will be able to search and then click on the searched item to add the corresponding id to the their list.

This is the following code I have written.

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 {

     }
    });
  }

Method

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;
  }
});

Object of List

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

From here, I am trying to make a sub/pub that shows ONLY the companies that are added in list.companies.

I have no idea on how..

I am thinking of something like this.

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

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

Everything I've written works so far. But I am lost in returning a cursor from an array of _ids.

UPDATE

Simply put, can I return a cursor from multiple _ids as query??


Solution

  • Yes, you can return a cursor from a list of ids. You can even return a cursor on any valid query.

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

    Is that what you were looking for?