Angular subscriptions


#1

Hello guys!

I have this weird issue, its kinda difficult to explain but I’ll do my best.

On the server side code I have 2 publications on one collection. One is for the grid pagination which limits the query and has current page, now the other one is to return single item, same like findOne() but with just find() to be able to get into one of those items on the pagination grid. On client side I have 2 views, one for the grid with pagination and other view is to view single item. Because I expect the grid to have many docs I limit server side query to display only certain fields but for some reason other publication which meant to show single item inherits those limited fields, but it only inherits if I subscribe it within the component constructor -> this.subscribe(‘publication’) ( problem with subscribing inside constructor has another issue, I have to query from another collection and on page reload this.helpers({item}) > item becomes undefined so cannot do second query ), otherwise if I subscribe within ui router resolve -> Meteor.subscribe(‘publication’) it works fine, but then after each findOne query grid publication inherits those items into the grid query so instead of seeing 15 limited items it adds the findOne item + 15 items…

If there is anything I could provide to help understand this issue please comment bellow and I’ll do my best!

Thanks guys!


#2

It sounds like you just need to adjust your subscriptions a bit to keep them from interfering with each other, but it’s difficult to tell without seeing your code. Can you post code samples (or repo) that outline the issue? Specifically where/how you’re calling your subscribe functions.


#3

Thank you for quick response hwillson!

Publication:

if (Meteor.isServer) {
Designs._ensureIndex({
’$**’: ‘text’
});
Meteor.publish(‘designSingle’, (findOneId) => {
check(findOneId, String);
return Designs.find({ styleNumber: findOneId });
});
Meteor.publish(‘designsList’, (indexSearch, Gridlimit, CurrentPg) => {
check(indexSearch, Match.OneOf(String, ‘’));
check(Gridlimit, Number);
check(CurrentPg, Number);

	if(indexSearch) {
		return Designs.find({
			$text: {
				$search: indexSearch
			}
		},
		{
			limit: 15,
			fields: {
				score: {
					$meta: 'textScore'
				}
			},
			sort: {
				score: {
					$meta: 'textScore'
				}
			}
		});
	} else if (Gridlimit || CurrentPg) {
		let limit = Gridlimit || 15;
		let skip = (CurrentPg - 1) * Gridlimit;
		return Designs.find({}, {
            sort: {
            	createdAt: -1
            },
			fields: {
				'createdAt': 1,
				'styleNumber': 1,
				'styleRefNo': 1,
				'date': 1,
				'style.sizerange_id.name': 1,
				'style.sizerange_id._id': 1,
				'style.basesize_id.name': 1,
				'style.designer_id.name': 1,
				'style.season_id.name': 1,
				'style.customer_id.name': 1,
				'style.status_id.name': 1,
				'style.description': 1
			},
			limit: limit,
			skip: skip
		});
	}
});

}

and my grid component subscription code:

this.subscribe('designsList', () => {
  return [ this.getReactively('indexQuery'),
           this.getReactively('query.limit'),
           this.getReactively('query.page')]
}, (err, success) => { this.preloader = false; });

and findOne subscription code: ( resolved in ui.router )

  resolve: {
    singleDesignSubs: ['$q', '$stateParams', function ($q, $stateParams) {
      let designId = $stateParams.designId;
      let deferred = $q.defer();

      Meteor.subscribe('designSingle', designId, {
        onReady: deferred.resolve,
        onStop: deferred.reject
      });

      return deferred.promise;
    }]
  }

This way grid list inherits every time I click on findOne item and keeps inherited findOne item at the top of the gridlist even while paginating further it wouldnt disappear… If I change my findOne subscription from ui.router into component:

constructor ($scope, $reactive, $stateParams, $timeout) {
‘ngInject’;
this.designId = $stateParams.designId;
this.subscribe(‘designSingle’, () => {return [this.designId]});
}

grid still inherits findOne item but on paginating it disappears and everything comes back to normal but other problem is that if I refresh browser while looking at individual item that item becomes undefined if consoling.log returned object and I cannot do findOne on another collection. If I navigate to any other page and back to single item it works again and object is being returned… does it make any sense?

Also thats my grid objects:

this.helpers({
  designsList() {
    if(this.indexQuery){
      return Designs.find({}, { sort: [['score', 'desc']] });
    }else{
      return Designs.find({}, { sort: { createdAt: -1 }, limit: this.query.limit });
    }
  },
  countDesigns(){
    return Session.get('designsCount');
  }
})

so findOne item sticks to designsList() }else{ object

Thank you!