Search code examples
meteorranking

Meteor create user ranking by score


I'm a total beginner. I'm using Meteor for a school project and I'm trying to create a ranking. I want to rank users according to their score. I want to display their usernames with their scores.

enter image description here

the function I found to sort the users and the following one

Meteor.users.find({}, { sort: { score: -1 } });

I would like to include it in the following code

Template.classement.helpers({

  users() {
    Meteor.users.find().forEach(function(oneUser) {
      const affichage = `nom : ${oneUser.username} score : ${oneUser.profile.score}`;
      console.log(affichage);
      console.log(oneUser.profile.score);
      console.log(oneUser);
    });

    return Meteor.users.find();
  },


});

on the html side, I have this to display but it dosen't work

<template name="classement">
  <h1>Classement</h1>
 {{#each users}}
    {{user}}
  {{/each}} 
</template>

Could you please help me. (sorry for the grammar, English is not my first language).


Solution

  • Welcome to SO!

    The issue is in the sort:

    Meteor.users.find({}, { sort: { score: -1 } });
    

    This attempts to sort documents by the top level field score. While you have score stored in a sub-document, under profile.

    The syntax to sort by an attribute of a submodule in MongoDB is this:

    Meteor.users.find({}, { sort: { 'profile.score': -1 } });