How to get attributes from a document in a collection from a template rendered?


#1

I am trying to build an application that uses D3. In my routes I have:

Router.route('/createNew/step2/:_id', {
  name: 'createNewStep2',
  layoutTemplate: 'appLayout',
  waitOn: function() {
    Meteor.subscribe('positions');
    return Meteor.subscribe('visualizations');
  },
  data: {
    'positions': function(){
       return Positions.find();
    },
    'visualizations': function(){
       return Visualizations.findOne(this.params._id);
    }
  }

});

Then, in the Template.createNewStep2.rendered = function() I want get the current visualization id to include as a parameter on a document of another collection.

I am trying:

 var visId = Visualizations.findOne({_id: this.params._id});

but i get an error.
So, the question is how I can get data from a collection from within a Template rendered function?


#2

Visualizations.findOne({_id: this.params._id}) will return a document, so your var ‘visId’ is an object and you’d need to do visId._id


#3

You need to use Template.currentData() inside rendered.


#4

lai, the router is passing data from 2 collections. Thus, how I can refer to the right one using the Template.currentData()?
When I include

 var currentData = Template.currentData();

in the Template.createNewStep2.rendered = function() I get an object that looks like:

Object {positions: function, visualizations: function}

Exploring that object I cannot find the data (both things inside: positions and visualization are functions). Thus, how can I get access to the attributes of the documents for the collections?

Can you give me a code example?


#5

Instead of doing:

data: {
  'positions': function(){
     return Positions.find();
  },
  'visualizations': function(){
     return Visualizations.findOne(this.params._id);
  }
}

You should do:

data: function () {
  return {
    'positions': Positions.find(),
    'visualizations': Visualizations.findOne(this.params._id)
  };
}

#6

lai, thanks. I finalloy solved the problem via a custom subscription.