Gaurav Agarwal
Gaurav Agarwal

Reputation: 19102

Could not render an array from two collections

Trying to build an app, like so

enter image description here

App has two collections

movies

{
    "_id" : "S7mGgtJhiQ3GZavqn",
    "cast_id" : [
        "pBnAFGaxNGLkDGuPk",
        "7HZkmd6BofNmjXRyw"
    ],
    "date" : "31-May-2014",
    "name" : "Rakshak",
    "vote" : 4
}

cast

{
    "_id" : "pBnAFGaxNGLkDGuPk",
    "link" : "http://en.wikipedia.org/wiki/Poonam_Dhillon",
    "name" : "Poonam Dhillon"
}
{
    "_id" : "7HZkmd6BofNmjXRyw",
    "link" : "http://en.wikipedia.org/wiki/Rishi_Kapoor",
    "name" : "Rishi Kapoor"
}

I have written the templates, like so

<template name="movies">
    {{#each movies}}
        {{> movie}}
    {{/each}}
</template>

<template name="movie">
    {{> vote}}
    <h3><span class="name"><b>{{name}}</b></span><br></h3>
    <span class="date"><b>Release Date:</b> {{date}}</span><br>
    <span class="cast"><b>Cast:</b></span>
    {{#each casts}}
        {{> cast}}
    {{/each}}
    <br>
</template>

<template name="casts">
    {{#each cast}}
        <a href="{{link}}">{{name}}</a>&#44;
    {{/each}}
</template>

and Template Managers

Template.movies.helpers({
    movies : function () {
      console.log("inside movies helper");
      return Movies.find();
    }

      });

  Template.movie.helpers({
    casts : function () {
      console.log("inside movie.helpers");
      console.log(this);
      return Cast.find({_id: this._id}) ;

    }

  });

I have tried a lot but could not render the names with links of the actors along Cast.?

Upvotes: 0

Views: 44

Answers (2)

Kuba Wyrobek
Kuba Wyrobek

Reputation: 5273

There are few fixes needed to make your example working, so I decided to create quick meteor project: https://github.com/parhelium/meteor-so-movies-cast

Result:

result

Fixed code:

<template name="cast">
    <a href="{{link}}">{{name}}</a>&#44;
</template>


Template.movie.helpers({
    casts : function () {
        console.log("inside movie.helpers");
        console.log(this);
        return Casts.find({_id: {$in:this.cast_id}}) ;
    }
});

Upvotes: 2

Marco de Jongh
Marco de Jongh

Reputation: 5448

Your code is not working because you are not using the cast_id array on the movie object. Instead you are trying to find cast with the _id associated with the movie. In the movie template helpers the data context is set to the movie object.

Instead you should set the data context to the cast_id entries for the cast templates like this:

Movie template:

{{#each cast_id}}
    {{> cast }}
{{/each}}

Cast template:

{{#with getCast}}
     DostuffwithData from getCast
{{/with}}

getCast template helper:

 Template.cast.getCast = function () {
      return Cast.find(this);
 }

Upvotes: 0

Related Questions