I’m not sure if I’m missing something obvious here, but I just noticed a glaring issue in a certain part of my app.
I have a page that displays information according to its URL, grabbing the details of a document that matches the parameter … except that the query always returns a result, even if the query doesn’t match.
Code is simple, so hopefully easy to debug:
The Publication:
Meteor.publish('prosByUri', function(uri) {
return Meteor.users.find({'profile.uri.active': uri}, {fields: {profile:1}});
});
The Route (with subscription):
Router.route("/:uri", {
name: 'New',
controller: 'NewController',
loadingTemplate: 'Loading',
waitOn: function () {
return Meteor.subscribe('prosByUri', this.params.uri);
},
action: function () {
this.render('New', {
data: {
uri: this.params.uri,
pro: function() {
var user = Meteor.users.findOne({});
if(user) {
return user;
}else{
Router.go('/error/404');
}
}
}
});
}
});
The strangest thing is that I’m sure this worked fine previously, I only noticed it by chance when I pasted some random string into the URL rather than the working URI - and it still gave me a user rather than redirecting to the 404.
Logging publication I can confirm that the URI is defined correctly, and that if it’s a non-existent URI, the collection returns no results (using fetch()
in the publication) - so where is the user coming from in these cases? Why isn’t it undefined?
Edit:
Ah, I’ve picked up on where the discrepancy is. If I’m logged in, and the publication finds no results, the subscription seems to use my logged in document - as if it were the result of the query. Is this expected? How do I avoid it? (I’m assuming it’s to do with the special nature of Meteor.users). I mean I could do a conditional check in the route, something like if ( Meteor.userId() === user._id )
- but I’m wondering if there’s an intended approach in these circumstances?