'this._id' return undefined when using {{#each .. in ..}}

Template.itemTemplate.events({
    "click tr": function() {
        console.log(this.item._id);
    }
});
3 Likes

Hiā€¦ One more question related to this, how to use data on UI template when helper fetch data with findOne?

Without each loop. If Iā€™m not wrong, #with can handle this but is it correct #with is not recommended to use as per documentation?

So what is the best practice to use single findOne data on template? Please advice, thank you.

When you use #with, you address just the properties of a given object, f.e. score property of player object. Without if, you have to address it as player.username. Of course, if your helper function is named player().

Thanks again for your reply.

I try console log the fetched data from helper, the data retrieved correctly but the UI {{player.profile.firstName}} is blank.

Again, what I did wrong?

you already using instance so donā€™t need to include object everytime, directly use property of objects.

use this object.

Show the code, both template and the helper.

Template.manageScore.helpers({
  player: function(){
    console.log(Meteor.users.findOne({_id:Session.get("selectedPlayer")}));
    return Meteor.users.findOne({_id:Session.get("selectedPlayer")});
  }
});
<template name="manageScore">
    <div class="ui container">
        <h1>Player:
            {{player.profile.firstName}}
        </h1>
    </div>
</template>

Try that:

Template.manageScore.helpers({
  player: function(){
    return JSON.stringify(Meteor.users.findOne({_id:Session.get("selectedPlayer")}));
  }
});

<template name="manageScore">
    <div class="ui container">
        <h1>Player:
            {{player}}
        </h1>
    </div>
</template>

What is the result? Do you see the userā€™s data?

Where and how did you use console.log that returned proper data?

I edited my helper above, added the console log that I use to print out the data. The following is what I get at console and Iā€™ve checked the properties, itā€™s what it should be.

Object
  _id: "WWYjCCiSmeKu8t8LM"
  createdAt: Sun Jan 15 2017 14:44:32 GMT+0800 (MYT)
  emails: Array[1]
  profile: Object
  roles: Array[1]
  services: Object__proto__: Object

Tried stringify, also empty at UI.

I discover one thing in the console. After the data is printed, console show another line showing undefined output from the same file/line that output the data.

Why that happen Iā€™m still investigating.

If I use find instead of findOne, the {{player}} display Player: [object Object]

Thatā€™s a cursor, which can be empty anyways.

Your findOne result gets changed, probably because of the Session changing. Write a helper that displays the value of this Session to compare it.

At server console I get the following:

Exception from sub driverInfo id ZjYEBxadk9JpLvYdX Error: Publish function can only return a Cursor or an array of Cursors

Show the code of your publication.

Was that displaying the whole time, or just appeared now?

Meteor.publish("playerInfo", function(playerId){
  return Meteor.users.findOne({_id:playerId});
});

I canā€™t be sure the error message appear all the time or not.

You cannot return with findOne() from publication. It has to be find(), always, even if you want to return just one object.

2 Likes

Ya, I just realised that. I got it working by changing the pub. Thank you so much @gusto for being so helpful. Very much appreciated.

1 Like