I found a helpful tutorial on building an Ionic/Meteor/Angular app and followed it to the point where I removed autopublish and insecure. Now, I'm diving into data publication and subscription in my project:
Inside my Meteor app's lib/collections.js
, I have two MongoDB collections:
Players = new Mongo.Collection('players');
Teams = new Mongo.Collection('teams');
A player has an _id
and a name
, while a team has an _id
and an array of exactly two players:
["someplayerId", "someotherPlayerId"]
In the server-side publications defined in server/publications.js
, I currently have:
Meteor.publish('players', function () {
return Players.find({});
});
Meteor.publish('teams', function() {
return Teams.find({}, {transform: function(doc) {
var playerOne = Players.findOne({ _id: doc.players[0]._id });
var playerTwo = Players.findOne({ _id: doc.players[1]._id });
doc.playerOne = playerOne;
doc.playerTwo = playerTwo;
return doc;
}
});
});
In my client-side routes configuration in client/routes.js
, I subscribe to these publications for one of my states:
.state('tab.settings', {
url: '/settings',
views: {
'tab-settings': {
templateUrl: 'client/templates/settings.html',
controller: 'SettingsCtrl as settings',
resolve: {
teams() {
return Meteor.subscribe('teams');
},
players() {
return Meteor.subscribe('players');
}
}
}
}
});
But when trying to use the teams data in my code, I am struggling to correctly access the compound data.
The following code snippet triggers an error because playerOne and playerTwo variables are undefined:
this.helpers({
players: () => {
return Players.find({});
},
teams: () => {
return Teams.find({}, {transform: function(doc) {
var playerOne = Players.findOne({ _id: doc.players[0] });
var playerTwo = Players.findOne({ _id: doc.players[1] });
doc.playerOne = playerOne.name;
doc.playerTwo = playerTwo.name;
return doc;
}
});
}
});
In the template file 'client/templates/settings.html'
, I want to utilize the transformed team documents. How can I achieve this?