Cosmo
Cosmo

Reputation: 13

Meteor - Template from multiple collections

I have two mongo collections:

And I would like to display the following template:

<template name="Postslist">
  {{#each posts}}
    {{>Post}}
  {{/each}}
</template>

<template name="Post">
  {{text}}
  {{name}}
</template>

So I have created a Post helper to display the associated name:

Template.Post.helpers({
  name: function() {
    var author = Authors.find({_id: this.authorid});
    return author.name;
  }
});

Unfortunately it does not display the author name like I would think. What am I doing wrong and how am I supposed to solve this kind of situation?

I know I'm not supposed to use foreign keys in NoSQL databases, but the collections are automatically populated by a 3rd party application and I cannot modify them.

Upvotes: 1

Views: 423

Answers (1)

Marco de Jongh
Marco de Jongh

Reputation: 5458

Find returns a mongo cursor not the matched documents. To get the matched documents from a cursor you have to use fetch however fetch is not reactive.

So to fix your problem and staying reactive you should return the result of findOne.

Template.Post.helpers({
    getAuthor: function() {
       return Authors.findOne({_id: this.authorid});
    }
});

Template changes:

<template name="Post">
    {{text}}
    {{#with getAuthor}}
        {{name}}
    {{/with}}
</template>

Upvotes: 1

Related Questions