Angular-Meteor can't access helper properties Angular 1

I have a list of campaigns and am pulling on together in a show page using a helper and having an issue accessing the campaign from the controller. It consoles: undefined.

angular.module('app').controller('CampaignShowCtrl', CampaignShowCtrl);
function CampaignShowCtrl($scope, $reactive, $stateParams){
  $reactive(this).attach($scope);
  this.subscribe('campaigns');
  this.helpers({
    campaign: () => {
      return Campaigns.findOne({_id: $stateParams.id});
    }
  });
  console.log(this.campaign);
}

This helper property works perfect in the template view. Just can’t access it from the controller. Is there something I’m missing?

I’m new to Meteor but am using a pattern like this that both waits on the results and only publishes the one campaign to the client.

self.subscribe('campaigns', () => [$stateParams.id], {
  onReady: function () {
    console.log('onReady: ' + self.campaign);
  }
});

self.helpers({
  campaign: () => {
    return Campaigns.findOne({_id: $stateParams.id});
  }
});
1 Like

I think my response above is not correct. While it will work sometimes, with larger data sets I’ve run into a timing issue.

I think this is the correct pattern when you need to wait on return of the collection from the server in your controller/component. Something like this:

self.subscribe('campaigns', () => [$stateParams.id], {
  onReady: function () {
    self.helpers({
      campaign: () => {
        var locCampaignsSub = Campaigns.findOne({_id: $stateParams.id});
        var locCampaigns = locCampaignsSub.fetch();
        console.log('onReady: ' + locCampaigns.length);
        return locCampaigns;
      }
    });
  }
});

this.helpers fetches the collection from the server asynchronously. This is the reason why it is not accessible to the controller at this stage.