Client shows all collections as empty - meteor

I am having trouble getting the client to find entries in a collection in a Meteor application.
This seems to be a common question and I have tried all the suggestions I have come across. For now, autopublish is included, so as far as I can tell the problem shouldn't be with publishing/subscribing.
I even went back and went through the simple-todo tutorial on meteor.com and meticulously checked each step. As soon as I replace my array with a collection, the collection comes up empty.
All I can think to do is wipe out my meteor install and reinstall, but I would really like to know what is causing this.
printTypes = new Mongo.Collection("printTypes");
if (Meteor.isClient) {
Meteor.subscribe("printTypes");
Router.configure({
loadingTemplate: 'homePage'
});
Router.map( function () {
this.route('itemPage', {
path: '/item',
waitOn: function() {
return Meteor.subscribe('printTypes');
},
data: function () {
templateData = { printTypes: printTypes.find({}) };
return templateData;
}
});
});
}
if (Meteor.isServer) {
Meteor.startup(function () {
Meteor.publish("printTypes", function () {
return printTypes.find();
});
});
}
Any ideas? Thanks!

Related

Deploying a meteor app from Meteor Galaxy Error: iron:router No route definitions found

I have deployed my meteor app from Meteor Galaxy. This works fine, but when I enter the address http://perfilesgs.meteorapp.com/, this shows me an error that the route has not been found.
If you need more information that I can give you to solve this problem tell me. I will be careful.
Thanks.
lib/router.js
var request = require('request');
var cheerio = require('cheerio');
var json2csv = require('json2csv');
Router.configure({
layoutTemplate: 'layout',
loadingTemplate: 'loading',
notFoundTemplate: 'notFound'
});
Router.route('/', {
name: 'home'
});
Router.route('/inicio', {
name: 'buscador'
});
Router.route('/results/', function () {
this.redirect('/inicio');
});
var requireLogin = function() {
if (! Meteor.user()) {
if (Meteor.loggingIn()) {
this.render(this.loadingTemplate);
} else {
this.render('accessDenied');
}
} else {
this.next();
}
}
Tested independently, the code you provided seems to work properly. I think that other errors in your code are preventing the client-side iron-router code from executing. I see two errors on the browser console:
Fix these (both seem trivial), and I imagine iron-router should work as expected. Update the question if this is not the case.

New to coding and using meteor. Deployment issue

I have created a very simple app to start out my learning process. I used the meteor deployment steps and I receive this error on the page.
Router.route('/', function () {
this.render('Home', {
data: function () { return Items.findOne({_id: this.params._id}); }
});
});
I have searched online about it and unfortunately I do not have the router knowledge to understand many of the answers. I will put my routing code here:
Router.configure({
layoutTemplate:'layout'
});
Router.route('/', function () {
this.render('home');
});
Router.route('/red', function () {
this.render('red');
});
Router.route('/yellow', function () {
this.render('yellow');
});
Router.route('/green', function () {
this.render('green');
});
Router.route('/home', function () {
this.render('home');
});
It routes fine in the localhost. If anyone could help me or poit me in the correct direction that would really help. I am extremely new to this (started last week) so please understand that. Thank you.
The issue was actually from the demo js that existed in the meteor template. Once I deleted the code from that js it works great!

meteor subscription manager change collection name

I'm using subscriptions manager with iron-router and my problem is this one.
I have a collection "participants" with 2 publications: allParticipants and todayParticipants.
if I go to this page:
Router.map(function () {
this.route('winners', {
waitOn: function () {
return [subs.subscribe('allWinners'),
subs.subscribe('allParticipants')];
console.log("subscribed!");
},
data: function () {
return {
winners: Winners.find(),
participants: Participants.find(),
loginBox: "True"
}
}
});
AllParticipants publication is subscribed and put in cache by the subscription manager package.
If after this, I go to this page:
Router.map(function () {
this.route('participants', {
path: '/',
waitOn: function () {
return subs.subscribe('todayParticipants');
},
data: function () {
return {
participants: Participants.find()
}
}
});
I'm expecting to subscribe only the todayParticipants but as my subscription is automatically named "Participants", It uses the cached subscription from the previous page being allParticipants.
Is there a way to change the name of my subscriptions in order to have each of them in the right cache?
Thanks.
What I do in my waitOn function is first stop my subscriptions like
if (App.subs) {
for (name in App.subs) {
App.subs[name].stop();
}
}
And then I create new subscriptions
App.subs = {
settings: Meteor.subscribe('settings', project),
...
};
return [App.sub.settings, .....];
Hope this helps!
Today, there seems to be no solution to this problem.
More explanation here: https://github.com/meteorhacks/subs-manager/issues/11
What I'm doing now is using a very limited number of subscriptions (filtered mainly on user) and then I create as much data objects as I want filtering my subscriptions in different ways.

Meteor Iron Router Run function when collection changes

Im new to Meteor and Im trying to figure out how to run a function after a collection change.
I have a route(iron router) that subscribes to a collection with waitOn. Which just waits for the subscrition to be ready before rendering which is what I want.
waitOn: function () {
return Meteor.subscribe('collection', this.params._id);
},
Any changes to the collection will be updated on all the clients and rendered automatically.
How would I run a function once the collection has changed?
You can use the onData hook, provided that you're returning that data using the data helper. E.g this is what a route may look like
this.route('routename',
path : '/abc',
waitOn: function () {
return Meteor.subscribe('collection', this.params._id);
},
data: function() {
return Collection.findOne({_id: this.params.id});
}
onData: function() {
//Do something when the data found by the above changes
}
});

How to properly replace this.stop() with pause() on Iron Router blaze integration

When I upgrade Iron Router to blaze integration branch, I began receiving this warning:
"You called this.stop() inside a hook or your action function but you should use pause() now instead"
Chrome console --> iron-router.js:2104 --> client/route_controller.js:193 from package
The code is on client:
Router.before(mustBeSignedIn, {except: ['userSignin', 'userSignup', 'home']});
var mustBeSignedIn = function () {
if (!Meteor.user()) {
// render the home template
this.redirect('home');
// stop the rest of the before hooks and the action function
this.stop();
return false;
}
return true;
}
I tried replacing this.stop() with: pause(), Router.pause() and this.pause() but still does not work. Also I haven't found pause function on iron-router package.
How do I properly replace this.stop() with pause()?
Thanks
From what I can tell the pause function is the first parameter your before hook is getting called with. Not in the docs anywhere, but that's what I gathered from the code and it seems to work.
Here's what I use:
var subscribeAllPlanItems = function (pause) {
var planId = this.params._id;
this.subscribe('revenues', planId).wait();
this.subscribe('expenses', planId).wait();
};
var waitForSubscriptions = function (pause) {
if (this.ready()) { //all the subs have come in
//NProgress.done();
setPlan(this.params._id);
} else { //all subscriptions aren't yet ready, keep waiting
//NProgress.start();
pause();
}
};
Router.map(function () {
this.route('calendar', {
path: '/calendar/:_id',
template: 'calendar',
before: [
subscribeAllPlanItems,
waitForSubscriptions
],
});
//Other routes omitted
});
var requireLogin = function (pause) {
if (Meteor.loggingIn()) { //still logging in
pause();
}
if (!Meteor.user()) { //not logged in
this.render('signIn');
pause();
} else { //logged in, life is good
console.log("requireLogin: logged in");
}
};
//This enforces login for all pages except the below ones.
Router.before(requireLogin, {
except: ['landing', 'signUp', 'signIn', 'forgotPassword', 'resetPassword']
});
I opened an issue on Github about this. Here's the response I got:
Oops I may have not changed the redirect method yet. Just use Router.go as it will work fine now. I will change over this.redirect sometime next week or a PR is welcome. Controllers are now automatically stopped if you change routes in a hook. You can pause the current run by calling the pause method which is passed as a parameter to your hooks and action functions.

Resources