How to return a value from a specific field to another collection in Meteor?

I bought a Discover Meteor book and went through a tutorial. I am still not sure about several sections and ran into a problem that I cannot work with.

I have two collections.

Computers = new Mongo.Collection('computers');
Apps = new Mongo.Collection("apps");

On the server, I publish:

Meteor.publish('computers', function() {
  return Computers.find();
});

Meteor.publish('apps', function() {
  return Apps.find();
});

On a customer subscription using Iron Router:

Router.configure({
  waitOn: function() {
    return [Meteor.subscribe('computers'), 
            Meteor.subscribe('apps'), 
            Meteor.subscribe('users')];
  }
});

In one collection, I referred to a document with the identifier of another document in another collection.

Computers.insert({
  _id: sd9f9sdf699,
  name: 'Mac1'
});

Apps.insert({
  _id: ewf4y34349f,
  name: 'App One',
  version: '1.0',
  computerId: sd9f9sdf699
});

Then I use the {{#each}} block to iterate the documents in the application collection

{{#each apps}}
  {{> appItem}}
{{/each}

<template name="appItem">
  <tr>
    <td><input type="checkbox" name="checked" class="ui checkbox"></td>
    <td>{{name}}</td>
    <td>{{version}}</td>
    <td>{{computerName}}</td>
  </tr>
</template>

and when I go to the computerId field, I would like to match the document in the computer collection and then return the computer name instead of id.

Here is my app_item.js code:

Template.appItem.helpers({
  computerName: function() {
    var id = this.computerId;
    var compName = Computers.find({_id: id}, {fields: {name: 1} }).fetch();
    return compName;
  }
});

I'm obviously missing something here, but I can't seem to plunge into what it is.

, , , , , , . , , , , .

!

+4
2

findOne() find(). find() coursor, fetch(), . findOne() .

Template.appItem.helpers({
  computerName: function() {
    var comp = Computers.findOne(this.computerId, {fields: {name: 1} });
    return comp.name;
  }
});

Meteor. : http://meteor.hromnik.com/blog/joins-in-meteorjs-and-mongodb

+4

, , - datamodel , computerid + computername . , - 2 1, . {{_}} .

, . , , mongo. ( ) - AppsAmount , . .

, .

Im , : this.computerId compname, ?

+2

All Articles