Search code examples
mongodbmeteoriron-router

Function returning data but object is undefined


I have a function that returns the most recent submission into a collection that is delivering data to the console but the object remains undefined. I think my code is correct but I think it might be Iron Router catching something?

Template helper

Meteor.subscribe("date");

Template.reportsBeforeDelete.helpers({
  aboutToDelete: function() {
return Foods.findOne({}, {sort: {createdAt: -1}, limit: 1});
  }
});

Publish

Meteor.publish('date', function(){
   return Foods.findOne({}, {sort: {createdAt: -1}, limit: 1});
});

HTML template

<template name="reportsBeforeDelete">
    <div class="foods">
      {{aboutToDelete}}
    </div>
    {{debug aboutToDelete}}
</template>

Router

Router.onBeforeAction(function() {
  if (! Meteor.userId()) {
    this.render('landing');
  } else {
    this.next();
 }
});

Router.configure ({
    layoutTemplate: 'home'
});

Router.route('/', {name: 'reports'});

Router.route('/activity'); 

Router.route('triggers');

Solution

  • You specified the complete document as return value

    {{aboutToDelete}}

    which attribute from the Foods collection should be rendered?...

    Another solution would be to define this in the route itself:

    Router.route('/foods/latest', {
      name: 'latestFoodsPage',
      data: function() { return Foods.findOne({}, {sort: {createdAt: -1}, limit: 1});}
    
    });