My current code below gets only 5 highest voted posts, showing the one with highest votes on top. What I want to do is, don't limit the post amount and only show posts with upvote count higher than 500. I want to sort them like, lowest voted post on top.
Template.top.helpers({
posts: function () {
return Posts.find({}, {sort: {votes: -1}, limit: 5});
}
});
PS: If this requires additional work besides the code above, any hints on how to proceed is well appreciated. The project is something I work on just to learn.
Thanks!
Give this a try:
Template.top.helpers({
posts: function () {
return Posts.find({votes: {$gte: 500}}, {sort: {votes: 1}});
}
});
It uses $gte to select the posts with votes >= 500.
Related
i am struggling with adding in pagination for my forums. Could you help me out? Basically, I was expecting on my forums page, to only see 10 posts. but it is returning all of them. The "Load More" button also does nothing (it seems like).
I am using this package: Paginated Subscription
Here is the code I am using:
if (Meteor.isClient) {
Deps.autorun(function() {
var handle = Meteor.subscribeWithPagination('posts',10);
});
Template.postsList.helpers({
'posts': function(){
return Posts.find({});
}
});
Template.postsList.events({
'click .btn': function(){
handle.loadNextPage();
}
})
}
if (Meteor.isServer) {
Meteor.startup(function () {
Meteor.publish("posts", function(limit){
return Posts.find({}, {limit: limit});
});
});
}
I don't think the var handle = Meteor.subscribeWithPagination('posts',10); Needs to be in the Deps block unless you are passing some reactive parameter to thehandle.
See this Issue on Github.
You can use Kurounin subscribe based pagination.It works for me.In atmospherejs, there is an another react pagination you can check it in Kurounin repository
I have already answered this kind of pagination to one of question. Please refer the exact code that can help you.
Meteor Pagination Issue
In your publication you must use sort (as documentation says)
// Using sort here is necessary to continue to use the Oplog Observe
Driver!
// https://github.com/meteor/meteor/wiki/Oplog-Observe-Driver
Meteor.publish("posts", function(limit){
return Posts.find({}, {
limit: limit,
sort: {
createdAt: -1
}
});
});
I'm not sure why this code works once in a while and fails other times:
var u = Meteor.users.findOne(username:'john');
console.log(u);
When I go to my page for the first time, sometimes the console.log(u) shows some results. But if I press refresh, console.log(u) shows undefined. I can't consistently reproduce one issue or the other. It seems pretty random when i get undefined or a collection. What's wrong with my code? How do I consistently get a collection for the variable u?
Like Christian Fritz said in comment on your question, it's probably a matter of collection not being fully loaded when your code is executed. If you use iron:router, you can use subscribe or waitOn as described there: http://iron-meteor.github.io/iron-router/#the-waiton-option so the page is loaded only when the collections are ready (meaning they are fully loaded).
You can also put it in a helper or use a Tracker Autorun to detect when your entry is available and then do whatever you want to do with it.
Edit: A sample for iron:router below
// myproject.jsx
var Cars = new Mongo.Collection('cars');
if(Meteor.isServer)
{
Meteor.publish("myCollections", function () {
return Meteor.users.find();
});
Meteor.publish("anotherCollection", function(){
return Cars.find();
});
}
//lib/router.js
Router.route('/my-page', {
name: 'myPage',
layoutTemplate: 'myPage',
waitOn: function() {
'use strict';
return [Meteor.subscribe('myCollection'),Meteor.subscribe('anotherCollection')];
},
data: function() {
'use strict';
return Collection.findOne();
}
});
I am new to Meteor and have a slight problem with Meteor.
I created a link which is
http://localhost:3000/game?id=7lJ8F
how would I get the id value in the query string and return it to a helper.
I have looked for answers but found none.
Router.route('/game/:_id', function(){
Session.set("gameid",this.params.query.id);
});
Template.gamebefore.helpers({
ids: function () {
return Session.get("gameid");
}
});
I know this is all wrong but I am quite desperate to find an answer so any will help. Thank You!
How to handle and get the parameters of an url are described in the guide of iron router
You might miss the _:
this.params.query.id
should be
this.params.query._id
or vice versa, since in your url is
/game?id=7lJ8F
in your route its
Router.route('/game/:_id', function(){
You don't need an id in the route:
Router.route('/game', function(){
Session.set("gameid",this.params.query.id);
});
Template.gamebefore.helpers({
ids: function () {
return Session.get("gameid");
}
});
As a note, instead of using http://..../game?id=somegameid, you might want to use a more REST approach by using http://.../game/somegameid, in which case, you need this route:
Router.route('/game/:_id', function(){
Session.set("gameid",this.params._id);
});
Template.gamebefore.helpers({
ids: function () {
return Session.get("gameid");
}
});
If I have a collection of posts when entering a view of a posts collection, and each of these posts has a collection of comments on them, how could I list the top comment for each post along side of them?
E.g:
this.route('postsList', {
path: '/:posts',
waitOn: function() {
return Meteor.subscribe('posts');
},
data: function() {
return Posts.find({});
}
});
And then I'm iterating through the collection of posts on a page.
{{#each posts}}
{{> postItem}}
{{/each}}
<template name="postItem">
{{title}}
{{topComment}}
</template>
I'd like to put the top comment for each post item.
How can I do this with my templates/subscriptions/publications?
Posts and comments are separate collections.
If it were an embedded collection I could see the ease of use but how to deal with separate collections?
If I published a recent comment type of publication how could I subscribe to it for each post as the most recent one? Or am I thinking the wrong way here?
If you insist on having two totally separated collections, you would get into problems with efficient database queries. What you could do is to have something like recentComment field in your posts collection. Should this field point to id of the most recent comment related to the given post, you could alter your posts subscription to include the recent comments as well:
Meteor.publish('posts', function() {
var listOfIds = _.pluck(Posts.find({}, {fields: recentComment}).fetch(), 'recentComment');
return [
Posts.find(),
Comments.find({_id:{$in:listOfIds}})
];
});
Note that this solution is not fully reactive but it's good enough in most cases.
I've got a sort filter working, in a helper, on a collection that shows "goals" ordered by date. I'm trying to add a filter that also only shows goals that have a status of 1.
/server/publications.js
Meteor.publish("goals", function() {
return Goals.find();
});
/client/main.js
Meteor.subscribe("goals");
/client/views/goals_list.js
Template.goalsList.helpers({
goals: function() {
return Goals.find({}, {sort: {submitted: -1}}, {status: 1});
}
});
The sort on submitted works fine, and continues working with the addition of the status, but I still see all the goals, not just the ones with a status of 1.
I've tried this, and many more ideas:
return Goals.find({}, {sort: {submitted: -1}}, {filter: {status: 1}});
Any help is much appreciated.
Directives which limit the returned documents based on their contents belong in the selector argument to find. So in your case:
return Goals.find({status: 1}, {sort: {submitted: -1}});