findOne isn't working

Hello

I write this code:

 'showSelectedPlayer': function () {
            var selectedPlayer = Session.get('selectedplayer');
            return PlayersList.findOne(selectedPlayer)
          }
    
            
     
<li>Selected Player: {{showSelectedPlayer.name}}</li>

and it should show the name of selected person but it isn’t working.
This tutorial is for one of the previous versions of meteor. Should I make changes in current version of meteor?
Can you please inform me what is wrong?

thanks

Saeed

What exactly is stored in the session variable? And did you subscribe to the collection?

The user id. Yes I have subscribed.
Here comes the tutorial:
http://meteortips.com/first-meteor-tutorial/forms/

my code:

import { Mongo } from 'meteor/mongo'
import { Session } from 'meteor/session'



PlayersList = new Mongo.Collection('players');
if (Meteor.isClient) {




    Template.leaderboard.helpers({

        'player': function () {
            return PlayersList.find({}, { sort: { score: -1, name: 1 } })
        },

        'selectedClass': function () {
            var playerId = this._id;
            var selectedPlayer = Session.get('selectedPlayer');
            if (playerId == selectedPlayer) {
                return "selected"
            }
        },


        'showSelectedPlayer': function () {
            var selectedPlayer = Session.get('selectedplayer');
            return PlayersList.findOne(selectedPlayer)
          
    
            
        }




    });

    Template.leaderboard.events({
        'click .player': function () {
            var playerId = this._id;
            Session.set('selectedPlayer', playerId);
        },
        'click .increment': function () {
            var selectedPlayer = Session.get('selectedPlayer');
            //console.log(selectedPlayer);

            PlayersList.update(selectedPlayer, { $inc: { score: 5 } });
        },
        'click .decrement': function () {
            var selectedPlayer = Session.get('selectedPlayer');
            PlayersList.update(selectedPlayer, { $inc: { score: -5 } });
        }

    });

    Template.addPlayerForm.events({

        'submit form':function(event){
            event.preventDefault();
            console.log("form submitted");
            console.log(event.type);
        }


    });


}

<head>
    <title>Leaderboard</title>
</head>

<body>
    <h1>Leaderboard</h1>
    {{>leaderboard}}
    {{> addPlayerForm}}
</body>

<template name="leaderboard">
    <ul>
        {{#each player}}
        <li class="player {{selectedClass}}">{{name}}:{{score}}</li>
        {{/each}}

        


        <li><button class="increment">Give 5 Points</button></li>
        <li><input type="button" class="decrement" value="Take 5 Points"></li>
        
        

        <li>selected player: {{showSelectedPlayer.name}}</li>


    </ul>
</template>

<template name="addPlayerForm">
    <form>
        
        <input type="text" name="playerName">
        <input type="submit" value="Add Player">

    </form>

</template>

Do you have a publication function somewhere to send the data from the server to the client?

1 Like

Yes, I have. You can see it in my code.
Should I install any package for this issue?

You forgot to add the server code for your publication. You are only showing us the client code.

1 Like

I don’t see you actually subscribing or publishing PlayersList. Do you still have the package autopublish installed in your project?

A publication function starts with Meteor.publish(

From memory, I think that in that tutorial, it ends up in a if (Meteor.isServer) { block in that same file

Thanks for all comments.
I get confused!! I’m not so familiar with meteor.

I did what this tutorial says: (you can see it at the bottom of the page).

http://meteortips.com/first-meteor-tutorial/databases-part-2/

Do you think this is wrong??!!

Well, that looks like a very poor resource - it does not tell you that you have to first create a publication, on the server. Further down, in that menu, I see Publish & Subscribe, which is what you have do first, but one would expect “Databases - part 2”, the section which you linked, to cover that. Instead, it only covers the client part.

I wholeheartedly recommend that you start with official resources, such as Meteor Tutorials. The docs are very well written, comprehensive, and easy to follow.

1 Like

Seems like selectedPlayer is an object with player’s data.

Try:
PlayersList.findOne({_id: selectedPlayer._id});

Can you check if you have the autopublish package installed? Or just run meteor add to be sure:

meteor add autopublish

I checked the leaderboard repo linked at the bottom of the page and they still have autopublish installed on that step

But yes, I also strongly recommend doing the official tutorial instead:
http://meteor.com/tutorials/

No, I didn’t know I should install autopublish!!
There was nothing about it in the resource!

Autopublish is installed out-of-the-box. Did you use --bare flag when creating new app? How did you started your app?

1 Like