Question

I created method to get an upcoming game from a collection of games. I include the current game id as an argument. The cursor is sorted by most 'hype points'(int) first, then from oldest to newest date.

getUpcomingGame: function(current_game_id, skip) {
  var current_game_hype = Games.findOne({_id: current_game_id}).hype;

  var upcoming_game = Games.find({hype: {$lte: current_game_hype}}, {sort: {hype: -1, submitted: 1}, limit: 10}).fetch()[skip + 1]; 
  // skip: 0) get the next game, skip: 1) get the next-next game and so on..

  return upcoming_game._id;
}

To get the next closest game, I could do: Meteor.call('getUpcomingGame', current_game_id, 0). The problem comes when the current game has the same 'hype points' as the previous game. If I use $lte, it could show me the current game (as the next game). On the other side, if I use $lt it bypasses the games with the same hype points but with a newest date.

Is there any way I could say, get the index of the current game within the cursor and return the next document. For this I would need to access to this cursor's indexing and I don't know if it's possible.

Was it helpful?

Solution

You could try iterating through the games array and removing any that match the current game.

Untested code:

//...

var upcoming_games = Games.find({hype: {$lte: current_game_hype}}, {sort: {hype: -1, submitted: 1}, limit: 10}).fetch();

var upcoming_games_modified = upcoming_games;

upcoming_games.forEach(function(game, index) {
    if(game._id === current_game_id)
        upcoming_games = upcoming_games_modified.splice(index);
});

return upcoming_games[skip + 1]._id;

OTHER TIPS

I think cursor.forEach does what you need.

http://docs.meteor.com/#foreach

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top